Given the root of a binary tree, return its level order traversal as a list of lists โ each inner list contains the values of nodes at that depth level, from left to right.
โข 0 โค number of nodes โค 2000โข -1000 โค Node.val โค 1000
02
Section Two ยท Approach 1
DFS with Depth Tracking โ O(n)
DFS (preorder), passing the current depth. Use a list of lists where depth is the index. Append values at their depth. Works but doesn't process nodes in a natural level-by-level order.
03
Section Three ยท Approach 2
BFS with Queue โ O(n)
Use a queue. At each level, process all nodes currently in the queue (that's one level), collect their values, and enqueue their children. The queue naturally separates levels.
๐ก Mental model: Imagine a stadium where each row is a level. You photograph row 1 (just the root), then row 2 (root's children), then row 3, etc. At each row, you snap all people in that row before moving to the next. The queue holds the current row.
Algorithm
Step 1: If root is null, return empty list.
Step 2: Initialize queue with root.
Step 3: While queue not empty:
levelSize = queue.size() โ snapshot current level count.
Process exactly levelSize nodes: dequeue, collect value, enqueue children.
Add collected values as a new level to result.
๐ฏ Key trick โ levelSize snapshot:
Before processing, capture queue.size(). This is how many nodes belong to the current level.
New children enqueued during this loop belong to the next level and won't be processed until the next iteration.
04
Section Four ยท Trace
Visual Walkthrough
BFS level-by-level โ queue snapshot per level
05
Section Five ยท Implementation
Code โ Java & Python
Java โ BFS with Queue
import java.util.*;
classSolution {
publicList<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = newArrayList<>();
if (root == null) return result;
Queue<TreeNode> queue = newLinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int levelSize = queue.size();
List<Integer> level = newArrayList<>();
for (int i = 0; i < levelSize; i++) {
TreeNode node = queue.poll();
level.add(node.val);
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
result.add(level);
}
return result;
}
}
Python โ BFS with deque
from collections import deque
classSolution:
deflevelOrder(self, root):
if not root: return []
result = []
queue = deque([root])
while queue:
level = []
for _ in range(len(queue)):
node = queue.popleft()
level.append(node.val)
if node.left: queue.append(node.left)
if node.right: queue.append(node.right)
result.append(level)
return result
06
Section Six ยท Analysis
Complexity Analysis
Approach
Time
Space
BFS Queue
O(n)
O(n) โ queue holds widest level
DFS + depth index
O(n)
O(h) stack + O(n) result
07
Section Seven ยท Edge Cases
Edge Cases & Pitfalls
Case
Input
Why It Matters
Empty
null
Return []. Check before enqueuing.
Single node
[1]
Result: [[1]]. One level.
Skewed left
1โ2โ3
Result: [[1],[2],[3]]. One node per level.
Complete tree
Full binary tree
Widest level has n/2 nodes โ queue peaks at O(n).
โ Common Mistake: Not capturing queue.size() before the for-loop. If you use queue.size() directly in the loop condition, it changes as you add children โ mixing levels in the same list.