Minimum Depth of Binary Tree (LeetCode 111): Solution and Explanation
Find the number of nodes on the shortest path from the root down to any leaf of a binary tree. An empty tree has depth 0, and a leaf is a node with no children at all.
Watch it run step by step
Try your own input and write the code →Intuition
The obvious recursion copies maximum depth and swaps max for min: the minimum depth is one plus the smaller of the two subtree depths. That is almost right, and the almost is the whole point of this problem. If a node has only a right child, its left depth is 0, and min would happily pick that 0 even though there is no leaf on the left side at all.
You can patch the recursion by ignoring a missing child, but it still has a deeper inefficiency. A DFS has to explore the entire tree before it can be sure which leaf is closest, even if a leaf sits right below the root while the other side is thousands of nodes deep.
Breadth-first search turns the question around. It explores nodes in order of their depth, all of depth 1 first, then all of depth 2, and so on. That means the first leaf the BFS pops is guaranteed to be the shallowest leaf, and you can return its depth immediately without looking at anything deeper.
To know each node's depth when it is popped, store it in the queue alongside the node. The root goes in as (root, 1), and every child goes in with its parent's depth plus one.
Approach: BFS with early exit at the first leaf
- If
rootisNone, return 0. - Create a deque holding the pair
(root, 1). - Pop the front pair
(node, depth). - If the node has no left and no right child, it is the closest leaf, so return
depth. - Otherwise enqueue each existing child with
depth + 1and keep looping. - The final
return 0is unreachable for a non-empty tree, since every tree has a leaf, but it keeps the function total.
Step-by-step example
root = [2,null,3,null,4,null,5,null,6]
- The queue starts as [(2,1)]. Node 2 has a right child, so it is not a leaf. Enqueue (3,2).
- Pop (3,2). Node 3 has a right child, so enqueue (4,3).
- Pop (4,3), not a leaf, so enqueue (5,4). Then pop (5,4), not a leaf, so enqueue (6,5).
- Pop (6,5). Node 6 has no children, so it is the first leaf found, and the answer is 5.
- A buggy min-based recursion would return 1 here, because the root's missing left child looks like a depth of 0.
Code: Python, Java, C++
1class Solution:
2 def minDepth(self, root: Optional[TreeNode]) -> int:
3 if not root:
4 return 0
5
6 q = collections.deque([(root, 1)])
7
8 while q:
9 node, depth = q.popleft()
10
11 if not node.left and not node.right:
12 return depth
13
14 if node.left:
15 q.append((node.left, depth + 1))
16 if node.right:
17 q.append((node.right, depth + 1))
18
19 return 01class Solution {
2 public int minDepth(TreeNode root) {
3 if (root == null) return 0;
4
5 Queue<TreeNode> q = new LinkedList<>();
6 q.add(root);
7 int depth = 1;
8
9 while (!q.isEmpty()) {
10 int size = q.size();
11 for (int i = 0; i < size; i++) {
12 TreeNode node = q.poll();
13
14 if (node.left == null && node.right == null) {
15 return depth;
16 }
17
18 if (node.left != null) q.add(node.left);
19 if (node.right != null) q.add(node.right);
20 }
21 depth++;
22 }
23
24 return 0;
25 }
26}1class Solution {
2public:
3 int minDepth(TreeNode* root) {
4 if (!root) return 0;
5
6 queue<TreeNode*> q;
7 q.push(root);
8 int depth = 1;
9
10 while (!q.empty()) {
11 int size = q.size();
12 for (int i = 0; i < size; i++) {
13 TreeNode* node = q.front();
14 q.pop();
15
16 if (!node->left && !node->right) {
17 return depth;
18 }
19
20 if (node->left) q.push(node->left);
21 if (node->right) q.push(node->right);
22 }
23 depth++;
24 }
25
26 return 0;
27 }
28};Time and space complexity
O(n)
In the worst case the nearest leaf is on the last level, so every node is enqueued and dequeued once. When a shallow leaf exists, the search stops much earlier.
O(n)
The queue holds at most one level of the tree plus part of the next, which can be about n / 2 nodes for a complete tree.
Other approaches
Recursive DFS with the one-child fix (time O(n), space O(h))
Return 0 for None and 1 for a leaf. If only one child exists, return 1 plus that child's depth, and if both exist return 1 plus the minimum of the two. It is compact but always visits every node and uses stack space equal to the height.
Common mistakes
- Writing
1 + min(minDepth(left), minDepth(right))with no special case. A missing child returns 0 and wins the minimum, so a tree shaped like a chain gets depth 1. - Counting edges instead of nodes. A single-node tree has a minimum depth of 1, not 0.
- Checking for a leaf when a node is enqueued but tracking depth when it is dequeued, which can make the depth off by one.
- Using a stack instead of a queue. The early return is only correct because BFS visits shallower nodes first.
Frequently asked questions
Why is BFS better than DFS for minimum depth?
BFS visits nodes in increasing order of depth, so the first leaf it meets is the closest one and it can stop right there. DFS has to finish exploring every branch before it knows the minimum.
Why can't I just use min instead of max from maximum depth?
Because a missing child is not a leaf. With min, a node that has one child would report a depth through its empty side, which is not a real root-to-leaf path. You must ignore the missing side.
What is the time complexity of minimum depth with BFS?
O(n) in the worst case, when the shallowest leaf is on the deepest level. In practice it is often far less, because the search ends at the first leaf.
What is the minimum depth of a tree with a single node?
It is 1. The root itself is a leaf, and depth here counts nodes on the path rather than edges, so the path from the root to that leaf contains exactly one node.