Skip to content
AI360Xpert
Beta
Difficulty: HardTrees

Binary Tree Maximum Path Sum

Problem in Plain English

A path is any connected sequence of nodes where none repeats. Given the root of a binary tree with possibly negative values, return the largest sum over all non-empty paths, which need not pass through the root.

Problem Statement

A path in a binary tree is a sequence of nodes where each pair of consecutive nodes is joined by an edge, and no node repeats.

Given the root of a binary tree, return the maximum sum over all non-empty paths. The best path may start and end anywhere and does not need to pass through the root.

Constraints

  • The number of nodes in the tree is in the range [1, 30000].
  • -1000 <= Node.val <= 1000

Examples

Example 1

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

The best path is 15 -> 20 -> 7. Including the root would produce at most 34, so a path entirely below the root wins.

Example 2

Input
root = [-3]
Output
-3

The path must contain a node. The only valid choice is -3; an empty path with sum zero is not allowed.

Example 3

Input
root = [-8,-3,-5]
Output
-3

Every extra node makes a path worse. Choose the least negative single node, -3, rather than clamping the final answer to zero.

Example 4

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

Both child branches help: the path 2 -> 1 -> 3 sums to 6. The downward gain from root is only 4, illustrating why returning the root gain alone loses the answer.

Example 5

Input
root = [5,4,null,3,2]
Output
12

The left child has a complete two-branch path of sum 9, but only its gain 7 can extend to 5. The legal winning path is 3 -> 4 -> 5, sum 12; adding both grandchildren and 5 would create an illegal fork.

Intuition

A path can rise toward the root and then descend, but it cannot fork into three branches. Call its highest node the peak. At a peak, combine the node value with the best useful downward path from each child; a negative child contribution is better omitted. In Example 1, peak 20 combines 15 and 7 for 42. Its parent can extend only one of those branches, so 20 offers a gain of 35, not 42. This difference between a complete path and an extendable gain is the central invariant.

Approaches

Peak at Every Node

Solution Details

Reveal Peak at Every Node: intuition, complexity, and code

Hints

Hint 1
Split the problem per node: the best path through it versus the best downward path it offers its parent.
Hint 2
A downward path that sums below zero should contribute 0, since skipping it always scores higher.
Hint 3
The arc through a node uses both children, but the value returned upward may extend through only one.

Edge Cases

  • All values negative: every child gain may be discarded, but each candidate includes its peak, so the largest node value wins.
  • A single node: the only non-empty path is that node, even when its value is negative or zero.
  • A best path below the root: Example 1 returns 42 even though the root gain is 25 and its complete candidate is 34.
  • A fork below a parent: [5,4,null,3,2] must return 12, not the illegal sum 14 of all four nodes.
  • A negative prefix followed by positive descendants: a child search must keep exploring, since a later value can recover that loss.
  • A chain of 30000 nodes: explicit stacks avoid recursion limits; the repeated-search baseline still takes quadratic time.

Common Mistakes and Interview Tips

  • Returning a two-sided candidate to the parent, creating a fork instead of a simple path.
  • Returning the root downward gain as the answer, missing paths with two branches or paths entirely below the root.
  • Initializing best to zero or allowing an empty final path, which breaks all-negative trees.
  • Pruning a negative intermediate running sum during a downward search, even though positive descendants can make that branch useful.
  • Keying the gain map by node value instead of node identity; repeated values belong to distinct subtrees.
  • Leaving best in persistent instance state without resetting it, so a second call can reuse the previous answer.
  • Claiming logarithmic or constant space for the one-pass code: its map keeps every node gain even when the tree is balanced.

Key Takeaway

For tree path problems, distinguish the complete candidate that may use both children from the extendable gain that may use only one. Evaluate the candidate at every node, then use postorder to compute gains once and reuse them above. Explicit traversal stacks preserve this invariant without recursion depth limits.