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.
Before you code
The depth of a tree is one more than the deeper of its two subtrees, and an empty tree has depth 0. That is the whole recursive solution.
Hints
depth(None) = 0;depth(node) = 1 + max(depth(left), depth(right)).- Iterative version: count the levels of a level-order traversal.
- Note that this problem counts nodes, so a single node has depth 1; the lesson counts edges. Say which you mean.
Production & complexity notes
When solving Maximum depth of binary tree on LeetCode (easy), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Vocabulary and traversals to reinforce the underlying pattern.
What it practices
Recursion on subtrees, from Vocabulary and traversals.