core practice · coding problem

Binary tree level-order traversal

Return the values of a binary tree grouped by level, top to bottom.

The mechanism

A tree is recursion made concrete: almost every tree problem is "solve it for the children, then combine". DFS (pre/in/post-order) is a recursion or an explicit stack; BFS is a queue, level by level. Pick the traversal that matches what you need to combine.

Study Trees

Expected evidence

levelOrder of [3,9,20,null,null,15,7] -> [[3],[9,20],[15,7]]

Try this drill in the practice workspace →

Hints when you need them
  • Levels mean BFS — use a queue.
  • Process the queue one level at a time by capturing its size before the loop.

Watch for these mistakes

  • Forgetting the null/empty base case
  • Using DFS when the problem is about levels or shortest depth (use BFS)
  • Returning the wrong thing from recursion — separate "answer" from "info passed up"

Check the sources

Practice the same mechanism

Browse all practice briefs