2Brute2Best
🔥0

Binary Tree Maximum Path Sum

Hard·LeetCode #124 Pro OnlyInterview-Level
LeetCode
01

Problem Statement

You are given the root of a binary tree. A path is a sequence of nodes where each pair of neighbours is joined by an edge. Each node can appear in a path at most once.

A path does not have to start or end at the root, and it does not have to go through the root. Its path sum is the total of the node values on it. Node values can be negative.

Return the largest path sum over all paths. A path must contain at least one node.

Input

The root of a binary tree. It has between 1 and 3 × 10^4 nodes. Values can be negative.

Output

The largest path sum of any non-empty path.

Example 1
Input: root = [1, 2, 3]
Output: 6

The best path is 2 → 1 → 3, with sum 2 + 1 + 3 = 6. It goes through the root.

Example 2
Input: root = [-10, 9, 20, null, null, 15, 7]
Output: 42

The best path is 15 → 20 → 7, with sum 42. It does not include the root, which has value −10.

Example 3
Input: root = [1, 20, null, 15, 7]
Output: 42

The best path is still 15 → 20 → 7. It cannot also continue up to 1, because a path that goes through 20 on both sides cannot go up to 20's parent.

Constraints
  • • The number of nodes is in the range [1, 3 × 10^4].
  • • −1000 ≤ Node.val ≤ 1000
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“path can start and end at any node”
→ Not root-to-leaf. The best path can bend at any node, so every node is a possible top.
“node values can be negative”
→ A side with a negative sum must be dropped. This is the max(0, ·) clamp.
“largest path sum”
→ Maximise a sum over paths. Each node returns a value upward and records a candidate on the side.
03

Approach Complexity Ladder

Brute Force — best downward gains at every node, each with a separate searchO(N²)O(H)

Treat each node as the top of the path, and search each of its sides from scratch

Optimal — return one side upward, check both sides at each nodeO(N)O(H)

One post-order pass: each node returns its best one-side gain and records the best path that bends there

Brute Force Intuition

Every path has a top node where it turns. For each node, find the best downward sum on its left side and on its right side, using a separate search for each side. Add the node's value and both sides (a side with a negative sum is dropped). The largest total over all nodes is the answer.

Recognition rule for this sub-pattern:

Each node returns a summary of its subtree (height, best path). The global answer is updated while combining.
Remember this · Tree DFS
Trigger
Paths, heights, diameters, validating structure, ancestors, anything "for every subtree".
Think
If my left and right children already gave me their answers, how do I build mine?
Key question
What does my recursive function return, and what do I pass down?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.