Loading...
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, a node with no children.
Note that the path must end at a leaf: a node with exactly one child is not a leaf, so the path has to continue through that child. The empty tree has depth 0.
Your function receives root as a TreeNode (or the empty tree, as null); the node type is provided for you, with val, left and right fields.
The examples below write the tree as its level-order traversal, where null marks a missing child of a listed node and trailing nulls are omitted. That is only how the input is displayed; the decoding is done for you.
Node.val ≤10003 is the root and 9 is a leaf two nodes down, so the shortest root-to-leaf path is 3 -> 9.
Every node has a single child, so the only leaf is the bottom node 6. A one-child node is not a leaf, and the path cannot stop early.
The empty tree has depth 0.
Click "Run" to test with sample cases or "Submit" to run all tests.