Skip to content
AI360Xpert
Beta
Difficulty: MediumTrees

Binary Tree Level Order Traversal

Problem in Plain English

Given the root of a binary tree, return its node values grouped level by level, from the root down to the deepest leaves, left to right within each level.

Problem Statement

Given the root of a binary tree, return the level order traversal of its node values.

Visit all nodes at each depth from left to right before moving to the next depth, grouping the values of each level into its own list.

Constraints

  • The number of nodes is between 0 and 2000, inclusive.
  • Each node value is between -1000 and 1000, inclusive.

Examples

Example 1

Input
root = [3,9,20,null,null,15,7]
Output
[[3],[9,20],[15,7]]

Root 3 is at depth 0. Its children 9 and 20 are at depth 1, in that order. Only 20 has children, so depth 2 contains 15 followed by 7; missing children contribute no placeholders.

Example 2

Input
root = [1]
Output
[[1]]

Root 1 is the only node, so its value forms one inner list. The result must be [[1]], rather than a flat [1].

Example 3

Input
root = []
Output
[]

There are no nodes or depths to report. Return an empty outer list, not a list containing an empty level.

Example 4

Input
root = [1,2,3,null,4,5,null]
Output
[[1],[2,3],[4,5]]

Node 4 is the right child of 2 and node 5 is the left child of 3. Their parents occur left to right as 2 then 3, so 4 must precede 5 despite the different child sides.

Intuition

Depth is the number of edges from the root. Grouping by depth turns Example 1 into root level [3], its child level [9,20], then [15,7]. A depth-first traversal can store each value in the bucket for its depth. A queue can instead finish the current frontier, the nodes waiting at one depth, before starting the next. In either case, visiting left children before right children preserves the required horizontal order.

Approaches

Recursive Depth Buckets

Solution Details

Reveal Recursive Depth Buckets: intuition, complexity, and code

Hints

Hint 1
A queue releases nodes in discovery order, which is exactly level order.
Hint 2
Read the queue length before draining a level so newly enqueued children do not join the current batch.
Hint 3
Alternatively, pass the depth down a recursive walk and append each value to the bucket for its depth.

Edge Cases

  • An empty root returns an empty list before any queue is created.
  • A skewed chain produces one singleton list per depth; recursion depth equals the node count.
  • Missing children are skipped without null placeholders, including the gaps in Example 4.
  • Duplicate and negative values still occupy separate output positions; do not deduplicate.
  • A broad tree uses a wide queue; a deep tree stresses recursion. Both must preserve every original node and child reference.

Common Mistakes and Interview Tips

  • Looping against the live queue length instead of a snapshot, which mixes two levels into one batch.
  • Using Python list pop(0) or JavaScript shift() repeatedly on broad frontiers can introduce quadratic copying; use deque or an index with level-boundary compaction.
  • Visiting right children before left children reverses required horizontal order, even though depth grouping remains correct.
  • Reusing the same inner list for several levels aliases mutable output. Allocate a fresh list for every level and keep results local to each invocation.
  • Confusing auxiliary space with returned output: recursion uses O(height), BFS uses O(width), and both allocate O(n) output.
  • Assuming default recursion stacks handle a 2000-node chain. Configure and restore Python recursion limits outside the helper, or choose the iterative queue approach.

Key Takeaway

Level-by-level work means breadth-first search with a size snapshot; the snapshot is what separates one level from the next.