Level-order traversal and horizontal tier exploration using queues
381 problems·40 Easy·235 Medium·106 Hard
Pattern Study Guide & Cheat Sheet· O(N) visits each node once · O(W) where W is maximum width of tree (up to N/2 at bottom level)
▼
Traverse tree level by level using a FIFO queue, processing all nodes at depth D before any node at depth D+1.
Core Invariant: Level Snapshot: At the start of each while loop iteration, `len(queue)` EXACTLY equals the number of nodes on the current level. Process that fixed count in a loop before touching the next level.
Recognize it (Keywords & Signals)
Level order traversal / zigzag level order
Binary tree right side view
Populate next right pointers in each node
Minimum depth of binary tree (earliest leaf found)
When NOT to use
Path sum from root to leaf or deep ancestor queries (Tree DFS is simpler and uses less auxiliary memory).
4.Loop `for _ in range(level_size)`: pop `node = queue.popleft()`, append `node.val` to `current_level`.
5.Enqueue children: `if node.left: queue.append(node.left)` and `if node.right: queue.append(node.right)`.
6.Append `current_level` to `result`.
Watch for (Interview Traps)
Using dynamic `len(queue)` directly in `while` loop condition without freezing `level_size`
Forgetting to check `if not root:` before creating queue (adds `None` to queue)
Using a Python list as queue with `pop(0)` (which is O(N) per pop!) instead of `deque.popleft()` (O(1))
Level-by-Level Tree BFS (Level Order Traversal)
from collections import deque
def level_order(root: TreeNode | None) -> list[list[int]]:
if not root:
return []
levels = []
queue = deque([root])
while queue:
level_size = len(queue) # Snapshot number of nodes at this depth
curr_level = []
for _ in range(level_size):
node = queue.popleft()
curr_level.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
levels.append(curr_level)
return levels
Cost
O(N) visits each node once · O(W) where W is maximum width of tree (up to N/2 at bottom level) (Optimal for level-oriented questions.)
Canonical problems
#102 Binary Tree Level Order Traversal:Standard level batching
#199 Binary Tree Right Side View:Last element in each level snapshot is visible from the right
#103 Binary Tree Zigzag Level Order Traversal:Reverse alternate level arrays or use deque
#111 Minimum Depth of Binary Tree:First leaf node encountered across levels guarantees minimum depth
Tree Breadth-First Search (BFS) frequently asked questions
What is the Tree Breadth-First Search (BFS) pattern in coding interviews?
Level-order traversal and horizontal tier exploration using queues It is categorized under Trees & Graphs and features 381 curated interview problems across Easy (40), Medium (235), and Hard (106) difficulties.
Which tech companies frequently test Tree Breadth-First Search (BFS)?
Companies frequently asking Tree Breadth-First Search (BFS) questions include Google, Amazon, Meta, Microsoft, Bloomberg, Uber.
How do I master the Tree Breadth-First Search (BFS) pattern?
Start by studying the algorithmic strategy and template code, then practice Easy and Medium problems before attempting Hard problems under timed conditions.