Maximum Depth of Binary Tree
Return the number of nodes on the longest root-to-leaf path.
Why does this pattern fit?
Restate the exact job
Return the number of nodes on the longest root-to-leaf path.
A node’s depth is one plus the deeper child depth.
O(n) time · O(h) stack
Mixing edge depth and node depth causes off-by-one results.
How to solve Maximum Depth of Binary Tree
The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.
What the question asks
Return the number of nodes on the longest root-to-leaf path.
Why Trees & recursive traversal fits
A node’s depth is one plus the deeper child depth.
State to maintain
The current node; null contributes zero.
Transition
Return 1 + max(depth(left), depth(right)).
Time and space
O(n) time · O(h) stack
Counterexample to the tempting mistake
Mixing edge depth and node depth causes off-by-one results.
Prove it again tomorrow
Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.