Problem. Given the root of a binary tree, return its maximum depth: the number of nodes along the longest path from the root down to a leaf.
3
/ \ → 3 (3 → 20 → 15)
9 20
/ \
15 7
empty tree → 0
It's the simplest "combine the children" problem, and the height it computes reappears everywhere: balance checks (lesson 10.6), diameters (lesson 10.5), the O(h) space cost of every recursive tree algorithm.
ML context. Depth is the most important hyperparameter of a decision tree: max_depth limits how many questions a
prediction can ask, which controls overfitting and inference latency (a prediction costs O(depth)). The depth of a
model's module tree, of a nested config or of a parse tree tells you how deep a recursive walk will go and whether it
will hit Python's recursion limit (Change C).