Minimum Depth of Binary Tree
Find the length of the shortest path from the root to any leaf.
Open on LeetCode ↗Intuition
Mirroring maxDepth with min(depth(left), depth(right)) looks natural but breaks the moment a node has only ONE child: the missing side reports depth 0, and min() happily returns 0, claiming a leaf exists where there is none. A leaf is a node with NO children at all -- when only one child is present you must recurse into that side exclusively and ignore the missing one. BFS sidesteps the whole trap: walking level by level, the very first leaf you dequeue is guaranteed to be at the minimum depth, since BFS never explores a deeper level before a shallower one is exhausted.
Approach
BFS level by level
Seed a queue with the root at depth 1. Process nodes in FIFO order, tracking each node's depth alongside it.
Stop at the first true leaf
The instant a dequeued node has no left AND no right child, its depth is the answer -- return immediately, no need to drain the rest of the queue.
Otherwise keep expanding
Push any existing children with depth + 1 and continue; a node with only one child is not a leaf and must not be mistaken for one.
Solution & live demo
Edge cases
Return 0 immediately, no nodes to search.
Root is dequeued first and has no children -- min depth 1.
BFS still finds the single leaf at the true bottom; a min()-based DFS would wrongly report 1.
First level with any leaf ends the search, often well before reaching every node.