Minimum Depth of Binary Tree
Minimum Depth of Binary Tree is LeetCode 111 (Easy). Return the number of nodes on the shortest path from the root down to the nearest leaf, a node with no children. An empty tree has depth 0.
The tree can hold up to 100,000 nodes, so a tree shaped like a chain is 100,000 levels deep.
- The number of nodes in the tree is in the range [0, 10⁵].
- -1000 <= Node.val <= 1000
Intuition
BFS visits a tree one level at a time: every node at depth 1, then every node at depth 2, and so on. So the first leaf BFS reaches is the shallowest leaf, and its level is the answer. The search stops there and never looks at anything deeper.
The one thing to get right is what counts as a leaf. A path that stops at a node with one child has not reached the bottom of the tree.
- Leaf: no left child and no right child, so its depth is the answer.
- Not a leaf: its children that exist go into the queue for the next level.
The shortest path from root to leaf is a nearest-target search, and BFS by levels answers it the moment it reaches the first target. The same shape finds the nearest exit in a maze or the fewest moves in a puzzle.
Approach
Before reading on, write down what a function should return for a node that has a left child but no right child. Then decide which traversal can stop as soon as it finds the answer. Aim for O(n) time in the worst case and an early exit when a shallow leaf exists.
Two ways to solve it
Check the tree level by level and return at the first leaf.
- Early exit: nothing below the shallowest leaf is visited.
- No recursion: a 100,000-node chain is safe.
The usual interview answer.
Ask both subtrees for their minimum depth and combine.
- Short code: three cases.
- Visits every node: no early exit.
- Watch out: a node with one child needs its own case.
Both are O(n) in the worst case. BFS wins because it stops at the shallowest leaf and needs no recursion; the recursive version is shorter but always visits the whole tree. The steps, code and live demo follow BFS, and the recursive code comes after the demo.
Return 0 for an empty tree
If root is missing there is no path at all, so the depth is 0. Otherwise put the root in a queue and set depth = 1, because the problem counts the nodes on the path, not the edges.
Take one level at a time
Note how many nodes the queue holds and pop exactly that many. Those are all the nodes at the current depth; children pushed meanwhile belong to the next level and wait behind them.
Return at the first leaf
When a popped node has neither child, return depth. Every shallower level was checked completely and had no leaf, so no leaf can be closer to the root.
Queue the children that exist
Otherwise push each child that is present. A node with only one child keeps the search going through that child, which is exactly how it differs from a leaf.
Move one level down
When a level ends without a leaf, add 1 to depth and repeat. The loop always ends with a return, because every non-empty tree has at least one leaf.
Code in Python, C++ and Java
Recursive DFS
A node with no left child can only continue to the right, so its depth is 1 plus the right subtree's depth, and the same holds the other way round. Only a node with both children takes the smaller of the two. An empty subtree returns 0.
Common pitfalls
Swapping max for min in the Maximum Depth formula
if not root:
return 0
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))if not root.left:
return 1 + self.minDepth(root.right)
if not root.right:
return 1 + self.minDepth(root.left)
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))A missing child returns 0, and min picks it, as if the path could end at a node that still has a child. For [1, 2] this returns 1, but the only leaf is node 2, at depth 2. On the chain [2,null,3,null,4,null,5,null,6] it returns 1 instead of 5.
Stopping at a node with one missing child
if not node.left or not node.right:
return depthif not node.left and not node.right:
return depthWith or, the BFS stops at the first node that lacks either child. For [1, 2] it returns 1 at the root, which still has a left child; the answer is 2.
Edge cases
Return 0 before touching the queue.
The BFS queue holds one node at a time, so the chain is no problem. The recursive version makes 100,000 nested calls, which is past Python's default limit of 1,000; its code raises the limit first.
Complexity
Minimum Depth vs Maximum Depth
The two problems look like mirror images, but only one of them can reuse the other's formula.
| Problem | Looks for | Recursive formula | BFS |
|---|---|---|---|
| 104 Maximum Depth | the deepest leaf | 1 + max(left, right) works: a missing child gives 0 and max never picks it over a real one | must finish every level |
| 111 Minimum Depth (this page) | the shallowest leaf | needs a case for one missing child, or min picks the 0 | stops at the first leaf |
Minimum Depth of Binary Tree FAQ
Is BFS or DFS better here?
BFS. If a leaf sits at depth 2 next to a branch 50,000 nodes deep, BFS checks a handful of nodes and returns 2, while recursive DFS walks all 50,000 nodes of that branch before it can rule it out. When every leaf is on the last level, the two do the same work.