Minimum Depth of Binary Tree
Go through the tree one level at a time with a queue. The first leaf we pop is the closest one, because levels come out from shallow to deep. Return the round number at that moment. Nothing deeper can beat it.
The first leaf popped from the queue is at the smallest depth of any leaf, so its round number is the minimum depth.
Problem
You are given the root of a binary tree. Return its minimum depth.
The depth of a node is the number of nodes on the path from the root down to it, so the root has depth 1. A leaf is a node with no children. The minimum depth is the depth of the closest leaf.
A node with only one child is not a leaf, so the walk cannot stop there.
The root of a binary tree. The tree can be empty.
One number: the minimum depth. An empty tree has depth 0.
Node 9 is a leaf at depth 2 (the path is 3 β 9). That is the closest leaf.
The tree is one chain: 2 β 3 β 4 β 5 β 6. The only leaf is 6, at depth 5. Node 2 has only one child, so it is not a leaf.
- β’ 0 β€ number of nodes β€ 10^5
- β’ β1000 β€ Node.val β€ 1000
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck β each hint is counted.
- 1.
Max Depth asks for the deepest node. This asks for the closest leaf. What changes in the code?
- 2.
Is a node with only one child a leaf? What should the walk do at that node?
- 3.
If I go level by level, which leaf do I meet first? Is that the closest one?
- 4.
Do I count depth once per node or once per round? What goes wrong if I count per node?
- 5.
When I find a leaf, can a later leaf still be shallower? How would I know?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Recurse to every leaf and keep the smallest depth seen | O(N) | O(H) |
| Optimal | Level-order queue; return the round number when the first leaf is popped | O(N) | O(W) |
Why does this belong to this pattern?
- βminimum depthβWe want a number of levels (nodes on a path), not a sum and not the path itself.
- βclosest leafβThe nearest end of a route. In a tree, BFS reaches nodes in order of distance, so the first match is the shortest.
- βa leaf has no childrenβA node with one child is not a stop. The leaf test has to check both children.
Maximum Depth (LC 104) asks for the deepest node, so a DFS with 1 + max(left, right) is the natural fit. This question asks for the closest leaf. BFS reaches nodes in order of depth, so the first leaf it pops is the shortest route. The one extra rule is the leaf test: a leaf has no children at all, so a node with one child is not a stop.
Don't make this mistake
This is the pattern-confusion trap. Maximum Depth uses max, and the copy changes it to min. But a missing child returns 0, and min picks that 0. On [2, null, 3, null, 4, null, 5, null, 6], node 2 has no left child, so min(0, β¦) is 0 and the code returns 1. The correct answer is 5: node 2 has one child, so it is not a leaf, and the walk must go down to 6.
|| makes a node with one child end the search. On [1, 2, 3, 4, null, null, 5, 6, 7], node 2 has only a left child (4). It is popped first in round 2, so the code returns 2. The correct answer is 3, from leaf 5.
depth has to be the level number. Increasing it per node makes a level with two nodes add 2. On [1, 2, 3, 4, null, null, 5, 6, 7], leaf 5 is the 5th node popped, so the code returns 5 instead of 3.
A left-first DFS takes the first leaf it reaches, not the closest one. On [1, 2, 3, 4, null, null, 5, 6, 7] it goes 1 β 2 β 4 β 6 and returns 4. Leaf 5 at depth 3 is the answer. Only a level-by-level walk meets shallow leaves first.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Tree BFS translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.