You are given the root of a binary tree. Your task is to find its minimum depth — the number of nodes along the shortest path from the root node down to the nearest leaf node.
A leaf node is a node with no left or right child.
Return the minimum number of nodes present on any root-to-leaf path.
Input: root = [3,9,20,null,null,15,7]
Output: 2
Explanation: The shortest root-to-leaf path is 3 → 9, which contains 2 nodes.
Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5
Explanation: The tree is skewed, so the only root-to-leaf path has 5 nodes.
Input: root = []
Output: 0
Explanation: An empty tree has a minimum depth of 0.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
1 2 3 4 5 6 7
3
10 5 15 2 7 12 20
3