Binary Tree Level Order Traversal
On this page
Problem statement
Given the root of a binary tree, return its values level by level: a list of lists, where each inner list holds one level's values from left to right.
The template for every "by level" tree question: right side view, zigzag order, averages per level, minimum depth. The only real trick is knowing where one level ends. Original problem: LeetCode 102.
Example

Input: [3, 9, 20, null, null, 15, 7]
Output: [[3], [9, 20], [15, 7]]
Input: []
Output: []
Constraints (assumed)
- 0 ≤ nodes ≤ 2,000.
What this question tests
- BFS on a tree with a queue.
- Separating levels cleanly using the queue size.
- Comparing BFS with a DFS that tracks depth.
Questions to ask first
- Left to right within a level? (Yes.)
- What should an empty tree return? (An empty list.)
- Top-down or bottom-up order? (Top-down; bottom-up is a common variant.)
- Any limits on memory? The queue can hold about half the nodes for a full tree.
Try it yourself first
You have the full problem. Sketch your answer, then sign up free to read the complete walkthrough.
- The complete design, flow by flow, with pictures
- Deep dives into the hard parts
- What mid, senior and staff candidates are expected to say
- Full company question lists
Free forever. No card needed.