Maximum Depth of Binary Tree

easyTreesrecursive DFS depthB75Solve on LeetCode#104

Part of Practice: Recursive Foundations

Given the root of a binary tree, return its maximum depth: the number of nodes on the longest downward path from the root to any leaf (a node with no children). An empty tree has depth 0 and a single node has depth 1.

The tree has at most 10,000 nodes.

Examples

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

The longest root-to-leaf path is 3 → 20 → 15 (or 3 → 20 → 7): three nodes.

Input:
root = [1,null,2]
Output:
2
Input:
root = []
Output:
0

Hints

One at a time — struggle first, peek second.

Your thinking space — verify on LeetCode, then mark it.