Maximum Depth of a Tree
L7 Medium Trees
Concept
A binary tree is naturally recursive: its depth is one plus the deeper of its two subtrees.
Given the root of a binary tree, return its maximum depth — the number of nodes along the longest path from the root down to the farthest leaf.
Examples
▸ root = [3, 9, 20, nil, nil, 15, 7]
→ 3
▸ root = [1, nil, 2]
→ 2
▸ root = []
→ 0
Progressive Hints
Hint 1 · Nudge
The height of a tree is one more than the taller of its two children's heights.
Hint 2 · Plan
Use the recurrence: the depth of an empty tree is 0, and otherwise it's 1 plus the larger of the left and right depths.
Hint 3 · Approach
If root is nil, return 0. Return 1 + the larger of depth(left) and depth(right).
All hints are out. Take a breath and give it a shot.
Output
// Run your code to see the output here.