Binary Tree Maximum Path Sum

Difficulty: Hard

You're given the root of a binary tree; node values can be negative. A path is a sequence of nodes connected by edges, with no node used twice. It can start and end anywhere and does not have to pass through the root.

Find the maximum sum of values along any path.

Examples

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

The path 2 -> 1 -> 3 uses every node and sums to 6, which is the best available.

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

The best path is 15 -> 20 -> 7, entirely within the right side of the tree, summing to 42. It doesn't touch the root at all, since -10 would only drag the sum down.

Constraints

  • The number of nodes is in the range [1, 3 * 10^4].

  • Node values are in the range [-1000, 1000].

Approach

Every path has one highest point, where it stops going up one side and starts down the other. (A path that goes straight down is the same idea, with one side adding nothing.)

The best path through a highest point is its value plus the best downward-only sum from each child, counting a side as 0 if it would only hurt.

The tricky part: a node can pass only one of those downward sums up to its parent, since a path can't branch two ways and still continue up.

Solutions

Brute Force — Recompute Downward Sums at Every Node

A helper maxDownward(node) returns the best sum starting at node and going straight down through one child, clamped to 0 (you can always skip a branch that would only subtract).

Treat every node as the path's highest point: its value plus maxDownward of each child. Take the best over all nodes. This is correct, but maxDownward is recomputed from scratch each time, redoing a lot of work.

class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function maxPathSum(root) {
  function maxDownward(node) {
    if (node === null) return 0;
    return Math.max(0, node.val + Math.max(maxDownward(node.left), maxDownward(node.right)));
  }

  function maxThroughNode(node) {
    if (node === null) return -Infinity;
    const throughThisNode = node.val + maxDownward(node.left) + maxDownward(node.right);
    return Math.max(throughThisNode, maxThroughNode(node.left), maxThroughNode(node.right));
  }

  return maxThroughNode(root);
}

Time: O(n^2) — maxDownward re-walks a subtree from every node above it · Space: O(h) — recursion stack, where h is the tree's height

Optimal — Single Pass with a Running Global Maximum

Do one post-order traversal (children before parent). The function returns the best downward-only sum through a node, which is what a parent needs to extend a path upward.

Along the way, at each node it also checks the best "bend here" path (value plus both children's downward sums) and updates one shared maximum. That maximum is only read at the end, so each node is computed exactly once.

class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function maxPathSum(root) {
  let maxSum = -Infinity;

  function maxDownward(node) {
    if (node === null) return 0;

    const leftGain = Math.max(0, maxDownward(node.left));
    const rightGain = Math.max(0, maxDownward(node.right));

    maxSum = Math.max(maxSum, node.val + leftGain + rightGain);

    return node.val + Math.max(leftGain, rightGain);
  }

  maxDownward(root);
  return maxSum;
}

Time: O(n) — every node is visited exactly once · Space: O(h) — recursion stack, where h is the tree's height