dsa · easy
Minimum Depth of Binary Tree
The **minimum depth** is the number of **nodes** on the shortest path from the root down to a **leaf**. A leaf has no children.
Arguments
root— binary tree as nested [val, left, right] or None
Each node is [val, left, right] or None. Return the minimum depth of root. The empty tree has depth 0. A single node has depth 1.
If a node has only one child, that node is not a leaf — you must keep walking through the child that exists.
Example
[3, [9, None, None], [20, [15, None, None], [7, None, None]]].
9 is a leaf at depth 2. 15 and 7 are leaves at depth 3. The shortest leaf path is through 9, so the answer is 2.
[1, [2, None, None], None] has no right child. 1 is not a leaf. The only leaf is 2, so the depth is 2 (not 1).
Constraints
Number of nodes in [0, 2000] -1000 <= val <= 1000 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [3, [9, None, None], [20, [15, None, None], [7, None, None]]] Expected: 2
Example 2
Input: [1, [2, None, None], None] Expected: 2