← Questions
Binary tree level order traversal
0
I was asked this
No reports yet. Sign in to say where you were asked it.
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
2000nodes (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
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
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

