Binary Tree Maximum Path Sum - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

We solve this using a single DFS traversal, where each node computes two key quantities. Before diving into those, let’s recall the definition of a path in this problem:

📖 A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can appear in the sequence at most once, and the path does not need to pass through the root.

Based on the path definition, any valid path can “turn” at most once — at a single node where traversal switches from one branch to another. We call that turning point the pivot. For simplicity, we’ll still use the term pivot even when one side is empty and no actual turn occurs — in either case, the pivot is the highest (closest-to-root) node on the path. Conceptually, the pivot is the only node that can combine contributions from both sides (when both child gains > 0); all other nodes on the path can contribute from only one side (left or right). Therefore, each valid path has exactly one pivot, serving as its rotation point.

Therefore, each node computes the quantities below:

  • 1️⃣ Local “pivoted” path sum (as if this node is the pivot)
    The best path that passes through the current node and is allowed to use both children:
    localPivotMaxPathSum = node.val + leftGain + rightGain
    This represents the maximum path sum that includes this node as the single pivot — the rotation point connecting both sides. We use this value to update the global maximum if it yields a better result than previously found paths.
  • 2️⃣ Gain returned to the parent
    Once we return to the parent, the current node can no longer act as the pivot. It passes up only a single-branch gain — its value plus the higher of its left or right gains:
    maxGainToParent = max(maxSingleBranchPathSum, 0)
    Here, maxSingleBranchPathSum = node.val + max(leftGain, rightGain) represents the best non-pivot (no rotation) path rooted at the current node that continues through only one child (left or right). If this sum is negative, we return 0 — meaning this node doesn’t contribute upward to its parent. Each child’s gain is already non-negative, since any negative branch was clamped to 0 during its own recursion.

Why two numbers? — Because each valid path can include only one pivot. Therefore, at each node we:

  1. Compute the best path that’s pivoted at the current node — to check if it improves the global maximum.
  2. Return only a single-branch gain to the parent — allowing ancestors to form their own pivoted paths independently, while getting the maximum contribution from this node.

🔁 DFS Recurrence

function maxGain(node):
    if node is null: return 0
    leftGain  = maxGain(node.left)
    rightGain = maxGain(node.right)

    localPivotMaxPathSum = node.val + leftGain + rightGain
    globalMaxPathSum = max(globalMaxPathSum, localPivotMaxPathSum)

    maxSingleBranchPathSum = node.val + max(leftGain, rightGain)
    return max(maxSingleBranchPathSum, 0) // calculate and return the maxGainToParent

📘 Variable Summary

  • localPivotMaxPathSum — best path sum through this node (pivoted here, allowed to use both sides).
  • maxSingleBranchPathSum — best single-branch gain returned to the parent (non-pivot contribution).
  • maxGainToParentmaxSingleBranchPathSum after dropping if negative.
  • globalMaxPathSum — overall best pivoted path found in the entire tree.

Time Complexity

O(n), where n is the number of nodes — each node is visited once.

Space Complexity

O(h), where h is the tree height — the maximum recursion stack size is proportional to the tree height.

Visucodize editorial solution titled JS Solution for LeetCode's Binary Tree Maximum Path Sum coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://leetcode.com/problems/binary-tree-maximum-path-sum.