← Questions

Binary tree level order traversal

0
MetaFigmaCodingLast asked Sep 2025

A coding interview question asked in Meta's technical phone screen and at Figma, last asked Sep 2025. It tests binary tree and bfs.

Given the root of a binary tree, return its values level by level: the root's level first, and each level from left to right.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • level_order(root: TreeNode | None) -> list[list[int]] — one inner list per level.

Example

      3
     / \
    9   20
       /  \
      15   7

level_order(root)   ->  [[3], [9, 20], [15, 7]]
level_order(None)   ->  []

The same traversal printed one level per line:

3
9 20
15 7

Constraints

  • Up to 2000 nodes (or many more; say how your answer scales).
  • -1000 <= val <= 1000.

Clarifying questions

  • Return a list of lists, or print each level on its own line?
  • Should empty positions show as null?
  • Is the tree very wide or very deep?

What a strong answer covers

  • Iterative BFS with a queue, processing one level at a time by reading the queue's size at the start of each level.
  • O(n) time, and O(w) space for the widest level w.
  • A DFS alternative that passes the depth and appends to levels[depth].
  • Tests: an empty tree, one node, a skewed tree, a complete tree.

Follow-ups

  • Print the levels in zigzag order, alternating direction.
  • Return only the rightmost value of each level (the right side view).
  • Number the nodes in level order, or connect each node to its right neighbor on the same level.

Where it's asked

  • Meta · Technical phone screen · Last asked Sep 202545 min · Two coding problems in CoderPad, about 20 minutes each · Usually no AI; some newer screens are AI-assisted
  • Figma · Coding · Last asked Oct 202260 min · One or two pair-programming rounds against provided tests

What it tests

binary tree · bfs

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding