Binary Tree Maximum Path Sum
Find the maximum sum of any connected path in a binary tree.
Why does this pattern fit?
Restate the exact job
Find the maximum sum of any connected path in a binary tree.
A parent can extend at most one child branch, while the global answer may join two.
O(n) time · O(h) stack
Returning both branches upward creates a fork, not a path.
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.