PROBLEM 31 OF 75

Binary Tree Maximum Path Sum

Find the maximum sum of any connected path in a binary tree.

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

Find the maximum sum of any connected path in a binary tree.

WHY THIS FITS

A parent can extend at most one child branch, while the global answer may join two.

COMPLEXITY

O(n) time · O(h) stack

COMMON FAILURE

Returning both branches upward creates a fork, not a path.

THE REASONING, IN ONE PLACE

How to solve Binary Tree Maximum Path Sum

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

Find the maximum sum of any connected path in a binary tree.

Why Trees & recursive traversal fits

A parent can extend at most one child branch, while the global answer may join two.

State to maintain

Best global path and each node’s best nonnegative downward gain.

Transition

Compute child gains, update best with node + both gains, return node + larger gain.

Time and space

O(n) time · O(h) stack

Counterexample to the tempting mistake

Returning both branches upward creates a fork, not a path.

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 ↗