102. Binary Tree Level Order Traversal
Asked at Google, Meta, Amazon, Microsoft and 4 more
Difficulty
Medium
Time
O(n)
Space
O(n)
Pattern
Breadth-First Search
Companies
8
Problem
You are given the root of a binary tree. Return the values of all nodes grouped by level, from the top level down to the bottom, and from left to right inside each level.
The answer is a list of lists. The first inner list holds the root's value, the second holds the values on the next level, and so on. If the tree is empty, return an empty list.
This is a breadth-first search (BFS) problem: you visit all nodes at one depth before touching any node at a deeper depth.
Group values by depth
Constraints
- ●
The number of nodes in the tree is in the range [0, 2000]. - ●
-1000 <= Node.val <= 1000
Examples
root = [3,9,20,null,null,15,7][[3],[9,20],[15,7]]root = [1][[1]]root = [][]Asked At
8 companies| Company | Difficulty | |
|---|---|---|
| Medium | View all Google questions → | |
| Meta | Medium | View all Meta questions → |
| Amazon | Medium | View all Amazon questions → |
| Microsoft | Medium | View all Microsoft questions → |
| Apple | Medium | View all Apple questions → |
| Netflix | Medium | View all Netflix questions → |
| Uber | Medium | View all Uber questions → |
| Atlassian | Medium | View all Atlassian questions → |
How to Think About It
Use a queue for BFS. Enqueue the root. Process all nodes at the current level before moving to the next.
The key trick: record the queue size at the start of each level. Process exactly that many nodes. This separates levels.
For each node processed: dequeue it, add its value to the current level list, enqueue its children (left then right).
This pattern extends to many tree problems: right side view (take last node per level), zigzag (alternate left/right), maximum width (count nodes per level).
Optimal Approach
Step 1: If root is null, return [].
Step 2: Initialize queue with root.
Step 3: While queue not empty:
- Record queue size (= nodes at current level)
- Process that many nodes:
Dequeue node- Add value to current level list
- Enqueue left child (if exists)
- Enqueue right child (if exists)
- Add level list to result
Step 4: Return result.
The for-loop over range(len(q)) at each level is critical. It processes exactly the nodes at the current level, not any newly added children.
Time: O(n). Space: O(n) for the queue.
BFS with a queue
What Trips People Up in Real Interviews
Confusing BFS with DFS. Level order traversal is BFS — use a queue. DFS (preorder/inorder/postorder) doesn't give you level-by-level grouping naturally.
Not processing all nodes at the current level before moving to the next. Use a for loop over the current queue size to separate levels.
Forgetting to handle null children. When adding children to the queue, only add non-null children.
Not handling an empty tree. Return an empty list, not null.
Using a stack instead of a queue. A stack gives you DFS (preorder), not BFS. Level order traversal requires a queue for FIFO processing to visit nodes level by level.
Solution Code
O(n) timefrom collections import deque
def levelOrder(root):
if not root:
return []
result = []
q = deque([root])
while q:
level = []
for _ in range(len(q)):
node = q.popleft()
level.append(node.val)
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
result.append(level)
return resultFrequently Asked Questions
What is the Binary Tree Level Order Traversal 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). This is the most common tree BFS problem in FAANG interviews.
How do you solve Binary Tree Level Order Traversal?
The optimal approach runs in O(n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Binary Tree Level Order Traversal?
Binary Tree Level Order Traversal is asked at Google, Meta, Amazon, Microsoft, Apple, Netflix, Uber, Atlassian. It is a medium difficulty problem.
What are common mistakes on Binary Tree Level Order Traversal?
- Confusing BFS with DFS. Level order traversal is BFS — use a queue. DFS (preorder/inorder/postorder) doesn't give you level-by-level grouping naturally.
- Not processing all nodes at the current level before moving to the next. Use a for loop over the current queue size to separate levels.
- Forgetting to handle `null` children. When adding children to the queue, only add non-`null` children.
- Not handling an empty tree. Return an empty list, not `null`.
- Using a stack instead of a queue. A stack gives you DFS (preorder), not BFS. Level order traversal requires a queue for FIFO processing to visit nodes level by level.