Advertisement

Minimum Depth of Binary Tree - LeetCode 111 Solution

Minimum Depth of Binary Tree - Complete Solution Guide

Minimum Depth of Binary Tree is LeetCode problem 111, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note: A leaf is a node with no children. Example 1: 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: The number of nodes in the tree is in the range [0, 10 5 ] . -1000 <= Node.val <= 1000

Detailed Explanation

Finding the minimum depth of a binary tree might sound straightforward, but it has a specific nuance that differentiates it from finding the maximum depth. The core of this problem lies in understanding what constitutes a 'leaf node' for the purpose of path termination. We're looking for the shortest path from the root down to any node that has no children at all. This means a node with only a left child (and no right) is *not* a leaf, and neither is a node with only a right child (and no left). The path must continue through that single child until an actual leaf is encountered. Consider Example 1: `root = [3,9,20,null,null,15,7]`. Here, node `9` is a leaf (no children), and its path from root `3` is `3 -> 9`, making a depth of 2. Nodes `15` and `7` are also leaves, but their paths `3 -> 20 -> 15` and `3 -> 20 -> 7` are both of length 3. Thus, the minimum depth is 2. If node `9` had, say, a left child, it wouldn't be a leaf, and we'd have to explore deeper. This strict 'no children' definition for a leaf node is the central point to grasp.

Solution Approach

The provided solution elegantly tackles this problem using a recursive Depth-First Search (DFS) pattern, carefully handling the edge cases that arise from the 'leaf node' definition. It works by asking each node to report the minimum depth to a leaf within its subtree. At its core, the logic is: if a node is empty (`null`), its depth is 0. If a node *is* a leaf (meaning it has no children), its depth is 1. The key trick comes when a node has *only one child*. If `root.left` is `null` but `root.right` exists, we *must* take the path through `root.right` because there's no leaf in the left subtree to terminate a path. Simply taking `min(depth(left), depth(right))` would incorrectly return `1` if `left` was `null` and `right` led to a deep subtree. Thus, if only one child exists, we recurse into that single child and add 1. Finally, if both children exist, we correctly take the minimum of the depths returned by the left and right subtrees, adding 1 for the current node. This specific branching logic ensures that we always follow a path that can eventually reach a *true* leaf node.

Step-by-Step Algorithm

  1. Step 1: Check if the root is null. If so, return 0 (empty tree).
  2. Step 2: Check if the root is a leaf node (no left and no right child). If so, return 1 (depth of a single node).
  3. Step 3: If only the left subtree exists, recursively calculate the minimum depth of the left subtree and add 1.
  4. Step 4: If only the right subtree exists, recursively calculate the minimum depth of the right subtree and add 1.
  5. Step 5: If both left and right subtrees exist, recursively calculate the minimum depth of both subtrees, take the minimum, and add 1.

Key Insights

  • The definition of a 'leaf node' as having *no children* is absolutely critical. Many common tree traversal patterns might treat a path ending at a `null` link as a 'depth', but for minimum depth, we must ensure the path terminates at a concrete node that *itself* has no further descendants.
  • Special handling for nodes with *only one child* is essential. If a node has a left child but no right child, the minimum depth to a leaf *must* come from its left subtree. Blindly applying `1 + min(minDepth(left), minDepth(right))` without these checks would incorrectly count a path ending at a `null` branch as a 'leaf path' if one child is `null` and the other leads to a very deep subtree.
  • The recursive structure allows us to build the minimum depth from the bottom up. Each recursive call effectively solves the 'minimum depth' problem for its subtree, and by combining these results with specific rules for single-child nodes and true leaves, the overall minimum path is accurately determined.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(h)

Topics

This problem involves: Tree, Depth-First Search, Breadth-First Search, Binary Tree.

Companies

Asked at: Adobe, Google, Meta, Uber.