The Runtime Theory
mediumleetcode#tree#bfs

Binary Tree Level Order Traversal

Return a binary tree's values grouped by level using a BFS queue that processes all nodes of one depth before the next.

The Runtime Theory Team1 min read
Solve it

solving happens on the judge — come back and mark it done

Sample cases

inroot = [3,9,20,null,null,15,7]

out[[3],[9,20],[15,7]]

inroot = [1]

out[[1]]

inroot = []

out[]

The machine must return the tree's node values grouped by depth, left to right within each level. Depth-first traversal produces a vertical order; this problem demands a horizontal one, which is exactly what breadth-first search gives you natively.

The key insight is that the queue's length at the start of a level tells the machine exactly how many nodes belong to that level. If it records that length before popping, then processes exactly that many nodes, it can slice the traversal into level-sized chunks without any depth bookkeeping.

The approach runs in three steps. First, if the root is null, return an empty list immediately. Second, push the root onto a queue and loop while the queue is non-empty; at each iteration, record the current queue size, pop that many nodes, append their values to the level list, and enqueue their non-null children. Third, append the finished level list to the result before starting the next chunk. Every node is enqueued and dequeued once, so time is O(n) and space is O(n) — the queue holds at most one full level, which can be the whole tree's leaves.

The trickiest edge case is the empty tree, which must produce [], not [[]]. The machine also must not enqueue null children — skipping them keeps the queue free of dead entries and the level boundaries accurate.

python
from collections import deque
 
def levelOrder(root):
    if not root:
        return []
    result, q = [], deque([root])
    while q:
        level = []
        for _ in range(len(q)):
            node = q.popleft()
            level.append(node.val)
            if node.left: q.append(node.left)
            if node.right: q.append(node.right)
        result.append(level)
    return result

More practice in this topic

One dispatch a week

The trace behind each problem, the tradeoff that explains it, and one technical dispatch per week — no noise.

One technical dispatch per week. No noise.