Minimum Depth of Binary Tree

Easy

Description

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. A leaf is a node with no children.

Examples

Input:root = [3,9,20,null,null,15,7]
Output:2
Explanation:

Shortest path is 3→9.

Input:root = [1]
Output:1
Explanation:

The tree has only one node (the root), which is also a leaf. The minimum depth is 1.

Input:root = [5,3,8,1,null,7,12]
Output:3
Explanation:

The tree has leaves at depths 3 (nodes 1, 7, 12). The shortest path from root to any leaf is 5→3→1 or 5→8→7 or 5→8→12, all with depth 3.

Constraints

  • 0 ≤ nodes ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!