Binary Tree Maximum Path Sum
Hard · Trees
Jump to codeFind the largest sum along any nonempty path in a binary tree. A path follows parent-child edges without revisiting a node; it need not include the root.
Examples
Example 1
root = [1,2,3]
Output: 6
Example 2
root = [-10,9,20,null,null,15,7]
Output: 42
Constraints
- 1 <= The number of nodes in the tree <= 1000
- -1000 <= Node.val <= 1000
Function contract
Trees use level-order arrays, with null for a missing child. Your code receives TreeNode objects with val, left, and right fields.
Need a hint?
A path returned to a parent can use only one downward branch.
Asked in real interviews
Candidates reported versions of Binary Tree Maximum Path Sum from these company interviews. Some company questions are locked and open with Premium or XP.
- Compute maximum path sum in a binary treeTikTok
- Find Maximum Path Sum in N-ary TreeNuro
- Maximum Path Sum in an Indexed Binary TreeNewrelic
Solution approach
At each node, compute the best nonnegative gain from each child. Update a global answer with node value plus both gains. Return node value plus the larger gain to the parent. Initialize the answer below every value so an all-negative tree still chooses a node.
Complexity
O(n) time and O(h) stack space.
Python reference solution
def maxPathSum(root):
best = float('-inf')
def gain(node):
nonlocal best
if node is None:
return 0
left = max(0, gain(node.left))
right = max(0, gain(node.right))
best = max(best, node.val + left + right)
return node.val + max(left, right)
gain(root)
return best
JavaScript solution
function maxPathSum(root) {
let best = -Infinity;
function gain(node) {
if (!node) return 0;
const left = Math.max(0, gain(node.left)), right = Math.max(0, gain(node.right));
best = Math.max(best, node.val + left + right);
return node.val + Math.max(left, right);
}
gain(root);
return best;
}
Java solution
import java.util.*;
class Solution {
public int maxPathSum(TreeNode root) {
int[] best = {Integer.MIN_VALUE};
gain(root, best);
return best[0];
}
private int gain(TreeNode node, int[] best) {
if (node == null) return 0;
int left = Math.max(0, gain(node.left, best)), right = Math.max(0, gain(node.right, best));
best[0] = Math.max(best[0], node.val + left + right);
return node.val + Math.max(left, right);
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int maxPathSum(TreeNode* root) {
int best = INT_MIN;
function<int(TreeNode*)> gain = [&](TreeNode* node) {
if (!node) return 0;
int left = max(0, gain(node->left)), right = max(0, gain(node->right));
best = max(best, node->val + left + right);
return node->val + max(left, right);
};
gain(root);
return best;
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 7 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[
[
1,
2,
3
]
]6
[
[
-10,
9,
20,
null,
null,
15,
7
]
]42
[
[
-3
]
]-3
[
[
2,
-1
]
]2
[
[
1,
2,
3,
4,
5
]
]11
Drafts are saved per language in this browser when storage is available.