Skip to main content

102. Binary Tree Level Order Traversal

mediumAsked at Pinterest

Binary Tree Level Order Traversal is a Pinterest tree-traversal warm-up: return node values grouped by level top-to-bottom. The interviewer wants BFS with a level-size snapshot — get the pattern right and they'll pivot to harder tree problems.

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

Source citations

Public interview reports confirming this problem appears in Pinterest loops.

  • Glassdoor (2026-Q1)Pinterest SWE phone-screen reports cite Level Order as the tree warm-up before a harder tree-DP problem.
  • LeetCode Pinterest tag (2026-Q1)On the Pinterest company-tagged problem list.

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

Example 3

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

Approaches

1. BFS with level-size snapshot (optimal)

Push root. While queue non-empty, snapshot the current size, then drain exactly that many nodes into the current level, pushing their children for the next iteration.

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

Tradeoff: Standard answer. The 'snapshot size before draining' pattern is what separates level-grouped output from a flat BFS list. Narrate the invariant.

2. DFS with explicit level parameter

Recursively descend with a level counter; on first visit to each level, push a new array; otherwise append.

Time
O(n)
Space
O(h) for recursion plus O(n) for output
function levelOrderDfs(root) {
  const result = [];
  function dfs(node, level) {
    if (!node) return;
    if (result.length === level) result.push([]);
    result[level].push(node.val);
    dfs(node.left, level + 1);
    dfs(node.right, level + 1);
  }
  dfs(root, 0);
  return result;
}

Tradeoff: Shorter to write but visits left-subtree fully before right-subtree, which feels wrong for 'level order' even though the output is identical. Some interviewers prefer the BFS version because it matches the problem statement's mental model.

Pinterest-specific tips

Pinterest interviewers pivot from Level Order to ALL the level-shaped tree problems: right-side view, zigzag, level averages. Volunteer 'I can adapt this snapshot pattern for X, want me to keep going?' the moment your basic version works. They want to test the variant fluency, not just one specific traversal.

Common mistakes

  • Forgetting the level-size snapshot — without it, you can't tell where one level ends and the next begins.
  • Using q.shift() in a tight loop on huge trees — O(n) per shift in V8. Use a head pointer or pre-allocate.
  • Returning a flat list instead of per-level arrays.
  • Not handling null root — early-return is required.

Follow-up questions

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

  • Binary Tree Right Side View (LeetCode 199): emit only the last value of each level.
  • Binary Tree Zigzag Level Order Traversal (LeetCode 103): alternate left-right and right-left per level.
  • Average of Levels (LeetCode 637).
  • Level Order Traversal II (LeetCode 107): same but bottom-up.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

BFS or DFS for this problem?

BFS matches the problem semantics and is the safe default. DFS with explicit level tracking works too and is sometimes shorter, but the BFS version is what Pinterest expects you to write first.

Why does Pinterest care about tree traversal?

Board / pin / topic hierarchies are tree-shaped. Many UI features (breadcrumbs, expansion state, sub-tree counts) are level-order traversal variants. Pinterest interviewers know the level-snapshot pattern transfers to dozens of follow-ups.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →