PROBLEM 23 OF 75

Maximum Depth of Binary Tree

Return the number of nodes on the longest root-to-leaf path.

PATTERNTrees & recursive traversalDefine the meaning of one recursive return value.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Return the number of nodes on the longest root-to-leaf path.

WHY THIS FITS

A node’s depth is one plus the deeper child depth.

COMPLEXITY

O(n) time · O(h) stack

COMMON FAILURE

Mixing edge depth and node depth causes off-by-one results.

THE REASONING, IN ONE PLACE

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.

Open blank recall ↗