Skip to content
CodeKruPro

Level order traversal of a binary tree

Coding problemEasy-MediumAsked in: Adobe, Amazon, Flipkart, Microsoft, Qualcomm, Samsung

Given the root of a binary tree, return its values level by level: one list per level, from the top level down, with the values of each level from left to right.

Trees are written in level order, with null for a missing child. An empty tree gives an empty list.

Examples

Example 1
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]
Example 2
Input: root = [1]
Output: [[1]]
Example 3
Input: root = []
Output: []

Constraints

  • The tree has between 0 and 100,000 nodes
  • Each value is a 32-bit integer

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in