Minimum Depth of Binary Tree
Easy
Topics
Given the root of a binary tree, return its minimum depth — the number of nodes along the shortest path from the root to the nearest leaf (a node with no children).
Example 1
The shaded path is the shortest root-to-leaf path, 2 nodes. The trap is a node with only one child: it is not a leaf, so its missing side must not be treated as depth 0.
Tree given in level-order (breadth-first);
null marks a missing child.Input: root = [3,9,20,null,null,15,7] Output: 2
Example 2
Input: root = [2,null,3,null,4,null,5,null,6] Output: 5
Constraints
- 0 <= number of nodes <= 10^5
- -1000 <= Node.val <= 1000
Editorial — intuition, approach, complexity and pitfalls
Intuition
Careful: the minimum depth must end at a leaf. A node with only one child cannot count the missing side as depth 0 — it must descend into the child that exists.
Approach
- Null node → depth 0.
- Leaf (no children) → depth 1.
- One child only →
1 + depth(that child). - Two children →
1 + min(left, right).
Dry Run
The two one-child branches are the entire problem. A missing child is not a
subtree of depth 0 to be minimised against -- it is no path at all, because the
depth has to end at a LEAF.
tree 3 9 20 null null 15 7
node 9 no children at all, so 1
node 15 leaf, 1
node 7 leaf, 1
node 20 both children, 1 + min(1, 1) = 2
node 3 both children, 1 + min(1, 2) = 2
answer 2 (the path 3 -> 9)
tree 2 -> 3 -> 4, a pure right spine
node 4 no left child, descend right only -> 1
node 3 no left child, descend right only -> 2
node 2 no left child, descend right only -> 3
answer 3
Replace all three branches with a plain 1 + min(left, right) and the first tree
still returns 2 -- it has no single-child node anywhere, so the bug is invisible
there. The spine exposes it: the answer drops from 3 to 1, because node 2's
absent left child reports 0 and wins the min. Even 1 -> 2 breaks, returning 1 for
a root that is not a leaf.
That is a bug that passes the obvious test case and fails the shape the problem
statement is actually warning about, which is why the one-child branches are
written out explicitly rather than folded into the min.Complexity
- Time — O(n): each node visited once.
- Space — O(h): recursion stack up to height h.
Common Pitfalls
- Using plain
1 + min(left, right)is wrong for a node with a single child — it would return 1 for a non-leaf.
Full Solution — all languages
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def minDepth(self, root):
if not root:
return 0
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))import java.util.*;
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int v) {
val = v;
}
}
class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
if (root.left == null) return 1 + minDepth(root.right);
if (root.right == null) return 1 + minDepth(root.left);
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}
}#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int v): val(v), left(nullptr), right(nullptr) {
}
};
class Solution {
public: int minDepth(TreeNode* root) {
if (!root) return 0;
if (!root->left) return 1 + minDepth(root->right);
if (!root->right) return 1 + minDepth(root->left);
return 1 + min(minDepth(root->left), minDepth(root->right));
}
};/**
* Tree node shape: { val, left, right }
* @param {TreeNode} root
* @return {number}
*/ var minDepth = function(root) {
if (!root) return 0;
if (!root.left) return 1 + minDepth(root.right);
if (!root.right) return 1 + minDepth(root.left);
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
};