Skip to content

Practice Binary Tree Maximum Path Sum

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#124
LeetCode Problem

Binary Tree Maximum Path Sum

Problem Statement

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 only appear in the sequence at most once. Note that the path does not need to pass through the root. The path sum of a path is the sum of the node's values in the path. Given the root of a binary tree, return the maximum path sum of any non-empty path.

Source: Binary Tree Maximum Path Sum on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

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

# Example 2

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

Constraints

The number of nodes in the tree is in the range [1, 3 * 10^4].
-1000 <= Node.val <= 1000

How to solve Binary Tree Maximum Path Sum

Original notes for interview preparation, written for this problem. Reviewed .

Key insight

Every path has a unique highest node where it bends. If each node knows the best downward-only sum from each child, the best path bending there is its value plus both non-negative child gains, while only one branch can be handed upward.

Why brute force is not enough

Enumerate every pair of nodes and sum the path between them through their lowest common ancestor. With up to 3 * 10^4 nodes that is roughly 4.5 * 10^8 pairs, each costing a walk proportional to tree height.

Approach

  1. Run a post-order depth-first traversal so that both children are fully evaluated before their parent.
  2. For each node, take the maximum downward gain from each subtree and clamp it to zero, since a negative branch is better left out.
  3. The candidate for a path bending at this node is its value plus both clamped gains; update a global best with it.
  4. Return to the parent the node value plus the larger single clamped gain, because a path continuing upward can use only one child branch.
  5. Initialize the global best to negative infinity, not zero, so an all-negative tree still reports the largest single node.

Walkthrough of Example 2

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

  1. Leaf 9 has no children, so its gain upward is 9 and the best so far becomes 9.
  2. Leaves 15 and 7 each report their own value upward; the best so far rises to 15.
  3. At node 20 the clamped gains are 15 and 7, so the bending candidate is 20 + 15 + 7 = 42, the new best. The gain passed upward is 20 + 15 = 35.
  4. At the root -10 the clamped gains are 9 and 35, giving a candidate of -10 + 9 + 35 = 34, which does not beat 42.
  5. The traversal ends and the answer is 42, the path 15 to 20 to 7.

Time and space

Time O(n) · Space O(h). Each of the n nodes is visited once with constant work; the recursion stack is proportional to the tree height h, which is O(n) for a degenerate chain.

Interview pitfalls

  • Initializing the running best to zero, which is wrong when every node is negative; for a single node valued -3 the answer is -3.
  • Returning both child gains to the parent, which would let a path fork at two different nodes.
  • Forgetting to clamp negative child gains to zero, dragging a harmful branch into the sum.
  • A skewed tree with 3 * 10^4 nodes can overflow the default recursion stack in some languages; mention an iterative post-order fallback.

Follow-up questions interviewers ask

  • How would you return the nodes on the best path rather than just its sum?
  • What changes if the path sum is defined over edge weights instead of node values?
  • Can you solve it without recursion to handle very deep trees safely?

Further reading on the technique: Depth-first search

Related LeetCode Problems

Practice more problems similar to Binary Tree Maximum Path Sum.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.