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.
A BFS approach counts completed levels as the queue drains. The recursive shape is equally valid and shorter: 1 + max(depth(left), depth(right)), with a null root returning 0.
[0, 10⁴].-100 ≤ Node.val ≤ 100Empty tree has depth 0; a single node has depth 1.
Depth is a question you can hand down the tree. You don't know how deep the whole tree is, but if each of the root's two children told you how deep its part goes, the answer would be one more than the bigger of the two. Each child can answer the same way, by asking its own children. The questions stop at an empty spot, which is 0 deep.
Time O(n)Space O(w)
The full walkthrough and code are part of Premium
3