Minimum Depth of Binary Tree

Easy

Topics
TreeDFSBFS

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

9315207
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

23456
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

  1. Null node → depth 0.
  2. Leaf (no children) → depth 1.
  3. One child only → 1 + depth(that child).
  4. 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))
SensAI
Beta
Tap the mic to stop voice mode