Given the root of a binary tree, return its maximum depth: the number of nodes on the longest path from the root down to any leaf.
An empty tree has depth 0; a single node has depth 1.
Examples
Input
root = [3, 9, 20, null, null, 15, 7]Output
3The longest root-to-leaf path is 3 → 20 → 15 (or 3 → 20 → 7): three nodes.
Input
root = [1, null, 2]Output
2The tree leans right; the path 1 → 2 has two nodes.
Constraints
0 ≤ number of nodes ≤ 10⁴-100 ≤ Node.val ≤ 100
The explainer is the good part.
Sign in for the full intuition build-up, every approach from naive to optimal with complexity analysis, progressive hints, and commented solutions in Python, JavaScript, and Java. All free.
Want a taste first? Try a free sample: Two Sum or Valid Parentheses.