Minimum Depth of Binary Tree

Given the root of a binary tree, return 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. Pattern focus: Recursive Tree Metrics. Be careful with nodes that have only one child.

Input Format

root = binary tree root

Output Format

minimum depth of the tree

Constraints

  • 1 <= number of nodes <= 10^5
  • -10^4 <= node values <= 10^4
  • Input must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

root = [3,9,20,null,null,15,7]

Output:

2

Explanation:

The nearest leaf is the node 9 at depth 2.

Example 2:

Input:

root = [2,null,3,null,4,null,5]

Output:

4

Explanation:

There is only one path from root to leaf.

Loading...
Minimum Depth of Binary Tree - Binary Tree