Skip to main content

64. Binary Tree Level Order Traversal

mediumAsked at Reddit

Return level-order traversal of a binary tree as a list of lists. Reddit uses this BFS warm-up to test queue technique — the same shape used in their comment-tree renderer which paginates by depth level.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit comments-team on-site question.
  • Blind (2025-11)Reported on Reddit comments-platform rounds.

Problem

Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).

Constraints

  • The number of nodes in the tree is in the range [0, 2000].
  • -1000 <= Node.val <= 1000

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
[]

Approaches

1. BFS with level marker

Queue + null markers between levels.

Time
O(n)
Space
O(w)
// Anti-pattern: null markers are bug-prone.

Tradeoff: Works but error-prone.

2. BFS with size-per-level (optimal)

Per iteration, capture queue size, drain that many. Push children. Each drain produces one level.

Time
O(n)
Space
O(w)
function levelOrder(root) {
  if (!root) return [];
  const out = [];
  const q = [root];
  while (q.length) {
    const size = q.length;
    const level = [];
    for (let i = 0; i < size; i++) {
      const n = q.shift();
      level.push(n.val);
      if (n.left) q.push(n.left);
      if (n.right) q.push(n.right);
    }
    out.push(level);
  }
  return out;
}

Tradeoff: Linear. The 'capture size at start of each level' pattern is the canonical BFS-level trick.

Reddit-specific tips

Reddit interviewers want the size-per-level trick. Bonus signal: connect to their comment-tree pagination — render level 0 immediately, paginate deeper levels via 'Show more comments' buttons.

Common mistakes

  • Reading q.length inside the inner loop (it changes as you push children).
  • Pushing null children (always check).
  • Returning a flat array instead of a list-of-lists.

Follow-up questions

An interviewer at Reddit may pivot to one of these next:

  • Zigzag (LC 103) — reverse alternate levels.
  • Bottom-up (LC 107) — same algorithm, reverse output.
  • Right side view (LC 199) — last node per level.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why capture size before draining?

We need to know exactly how many nodes are on this level before adding their children to the queue.

Could DFS do this?

Yes — DFS with depth parameter, appending to out[depth]. Same complexity, different traversal order.

Practice these live with InterviewChamp.AI

Drill Binary Tree Level Order Traversal and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →