Given the root of a binary tree, return its maximum depth — the number of nodes along the longest path from the root node down to the farthest leaf node.
root = [3,9,20,null,null,15,7]3root = [1,null,2]2root == null → return 0 (null node has depth 0)L = maxDepth(root.left)R = maxDepth(root.right)1 + Math.max(L, R) — this node adds 1 to the deeper subtreePost-order (left → right → root) processes children before the parent. By the time we ask "what is the depth of this node?", both subtree depths are already known. The depth of any node is simply 1 (for itself) + the deeper of its two subtrees. This naturally bubbles the answer up from leaves (depth 1) to the root. The call stack is the recursion — as it unwinds, values propagate back up.
You could list every root to leaf path and take the longest one. But storing every path is unnecessary, because only the length matters.
The depth of a tree is 1 for the root, plus the deeper of its two subtrees. An empty tree has depth 0, and that is the base case. So the recursion asks each child for its depth, and passes the answer up.
Depth equals 1 plus the deeper child. Counting levels with a breadth first search gives the same answer.
Run the animation above on your own input, or watch the 1:36 video explanation of Maximum Depth of Binary Tree with a dry run and Java code.