Lesson 137 of 296
Trees: breadth-first search
What you will learn
- Breadth-first search (BFS) and level-order traversal
- Processing one level at a time
- Binary Tree Level Order Traversal
- Right Side View
- Minimum Depth
- DFS or BFS: how to choose
On this page
Breadth-first search
BFS visits nodes level by level: the root, then all nodes at depth 1, then depth 2, and so on. It uses a queue (Lesson 129): take a node from the front, add its children to the back.
Binary Tree Level Order Traversal
Problem (LeetCode 102): return the values level by level.[3, 9, 20, null, null, 15, 7] → [[3], [9, 20], [15, 7]].
Key technique: at the start of each level, the queue contains exactly that level's nodes. Record levelSize = queue size, and process exactly that many nodes.
function levelOrder(root: TreeNode | null): number[][] {
const result: number[][] = [];
if (root === null) return result;
let level: TreeNode[] = [root];
while (level.length > 0) {
result.push(level.map((node) => node.val));
const next: TreeNode[] = [];
for (const node of level) {
if (node.left !== null) next.push(node.left);
if (node.right !== null) next.push(node.right);
}
level = next;
}
return result;
}This version uses one array per level instead of a single queue, which avoids shift() (Lesson 129) and makes the level boundaries obvious. O(n) time, O(w) space, where w is the widest level.
Binary Tree Right Side View
Problem (LeetCode 199): the values you would see looking at the tree from the right: the last node of each level.
function rightSideView(root: TreeNode | null): number[] {
const result: number[] = [];
if (root === null) return result;
let level: TreeNode[] = [root];
while (level.length > 0) {
result.push(level.at(-1)!.val);
const next: TreeNode[] = [];
for (const node of level) {
if (node.left !== null) next.push(node.left);
if (node.right !== null) next.push(node.right);
}
level = next;
}
return result;
}Minimum Depth of Binary Tree
Problem (LeetCode 111): the number of nodes on the shortest path from the root to a leaf.
BFS is ideal: the first leaf you meet is on the shallowest level, so you can stop immediately.
function minDepth(root: TreeNode | null): number {
if (root === null) return 0;
let level: TreeNode[] = [root];
let depth = 1;
while (level.length > 0) {
const next: TreeNode[] = [];
for (const node of level) {
if (node.left === null && node.right === null) return depth;
if (node.left !== null) next.push(node.left);
if (node.right !== null) next.push(node.right);
}
level = next;
depth++;
}
return depth;
}(A recursive DFS works too, but must handle nodes with only one child carefully: a missing child is not a leaf.)
DFS or BFS?
| Use BFS when | Use DFS when |
|---|---|
| You need things level by level | You need to explore full paths |
| You want the shortest path in steps (unweighted) | You need information from subtrees (height, sums) |
| The answer is probably near the root | The tree is very wide (BFS would store a whole level) |
| Backtracking-style problems |
Both visit every node once: O(n) time. Space: BFS O(width), DFS O(height).
How to say it in an interview
"BFS visits a tree level by level using a queue. I process one level at a time, either by recording the queue length or by building a separate array for the next level, which gives level order, right side view and similar problems in O(n). BFS is also the natural choice for the shortest path in an unweighted structure, like minimum depth, where I can stop at the first leaf. DFS uses O(height) space and BFS O(width)."
Key words
| Word | Meaning |
|---|---|
| BFS | Breadth-first search: level by level |
| Level-order traversal | Visiting a tree one level at a time |
| Width | The number of nodes in the widest level |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.