Tree Breadth-First Search Pattern
Tree BFS visits a tree level by level using a queue, processing every node at depth d before any node at depth d+1. It is the canonical pattern for level-order traversal, shortest-path-in-tree problems, and any question whose answer depends on horizontal slices of the tree, in O(n) time and O(w) space where w is the maximum width.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Complexity
- Time
- O(n)
- Space
- O(w)
When to use Tree Breadth-First Search
- The problem asks for level-order traversal, zigzag traversal, or right-side view of a tree.
- You need the minimum depth of a tree (BFS finds the first leaf without exploring the whole tree).
- You need to connect siblings or next-right pointers at each level.
- You need to compute a per-level aggregate (average, max, sum).
- The recursive call stack is too deep for the input size and an iterative approach is required.
Template
function levelOrder(root) {
if (root === null) return [];
const result = [];
const queue = [root];
while (queue.length > 0) {
const levelSize = queue.length;
const level = [];
for (let i = 0; i < levelSize; i++) {
const node = queue.shift();
level.push(node.val);
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
result.push(level);
}
return result;
}Example LeetCode problems
| # | Problem | Difficulty | Frequency |
|---|---|---|---|
| 102 | Binary Tree Level Order Traversal | medium | foundational |
| 103 | Binary Tree Zigzag Level Order Traversal | medium | frequently asked |
| 199 | Binary Tree Right Side View | medium | company favorite |
| 111 | Minimum Depth of Binary Tree | easy | foundational |
| 637 | Average of Levels in Binary Tree | easy | foundational |
| 116 | Populating Next Right Pointers in Each Node | medium | classic |
| 107 | Binary Tree Level Order Traversal II | medium | frequently asked |
| 104 | Maximum Depth of Binary Tree | easy | foundational |
Common mistakes
- Forgetting to capture `queue.length` before the inner loop, which causes children to leak into the current level as they get appended mid-iteration.
- Using `queue.shift()` in production code on huge inputs — it is O(n) per call on array-backed queues; prefer a real deque or a head-index pointer.
- Pushing null children into the queue and then crashing when dereferencing `.val` on the next iteration.
- Returning the wrong level shape (flat array vs array of arrays) — re-read the expected output format before submitting.
- Using BFS for maximum-depth problems where DFS recursion is simpler; BFS shines for minimum-depth and per-level computation.
Frequently asked questions
Why is BFS better than DFS for minimum-depth problems?
BFS visits nodes in order of increasing depth, so the first leaf it encounters is automatically the shallowest one. DFS would have to explore every root-to-leaf path and take the minimum, doing significantly more work on unbalanced trees.
What is the space complexity of BFS?
The queue holds at most one full level at a time. In the worst case (a complete binary tree) that's roughly n/2 nodes, so O(n). For typical trees the maximum width w is much smaller and the practical space is O(w).
How do I distinguish levels without a separator value in the queue?
Snapshot the queue length at the start of each outer iteration. That count tells you exactly how many nodes belong to the current level, so you can process them as a group before moving to the next level.
Can BFS handle n-ary trees?
Yes. Replace the `if (node.left); if (node.right)` lines with a loop over `node.children`. The level-size snapshot trick works identically.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill the Tree Breadth-First Search pattern under real interview conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →