Algo-Sim › Binary Trees & BST
Binary Tree Maximum Path Sum
Hard · Binary Trees & BST · step through it in Java, Python and C++ on your own input
Each node returns its best downward gain (dropping negative sides) while the answer tries every node as the top of a path that bends there.
Problem
A path is a sequence of nodes where each pair of neighbours is joined by an edge, and no node appears twice; it does not need to pass through the root. Its sum is the sum of its values. Given the root of a binary tree with at least one node, return the largest sum of any non-empty path.
Constraints
- 1 ≤ number of nodes ≤ 3 · 10⁴
- -1000 ≤ Node.val ≤ 1000
- Values can be negative, so the best path may be a single node
Edge cases to test
- All negative values: the single largest value
- One node
- The best path does not pass through the root
Follow-up questions
- Why drop a negative side?
- A path can stop at the node instead, so a negative gain only lowers the sum: use
max(0, gain). - Why does the function return one side only?
- A path coming up through a parent can’t take both branches; only the bent path at a node uses both.
Solution
Java
private int best = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
gain(root);
return best;
}
private int gain(TreeNode node) {
if (node == null) return 0;
int left = Math.max(gain(node.left), 0);
int right = Math.max(gain(node.right), 0);
best = Math.max(best, node.val + left + right);
return node.val + Math.max(left, right);
}Python
def max_path_sum(root):
best = float("-inf")
def gain(node):
nonlocal best
if not node:
return 0
left = max(gain(node.left), 0)
right = max(gain(node.right), 0)
best = max(best, node.val + left + right)
return node.val + max(left, right)
gain(root)
return bestC++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int best = INT_MIN;
int maxPathSum(TreeNode *root) {
gain(root);
return best;
}
int gain(TreeNode *node) {
if (node == nullptr)
return 0;
int left = std::max(gain(node->left), 0);
int right = std::max(gain(node->right), 0);
best = std::max(best, node->val + left + right);
return node->val + std::max(left, right);
}
};
More Binary Trees & BST problems
- Preorder Traversal (Recursive) (Easy)
- Inorder Traversal (Recursive) (Easy)
- Postorder Traversal (Recursive) (Easy)
- Preorder Traversal (Iterative) (Easy)
- Inorder Traversal (Iterative) (Easy)
- Postorder Traversal (Iterative) (Easy)
- Binary Tree Level Order Traversal (Medium)
- Binary Tree Zigzag Level Order Traversal (Medium)
- Binary Tree Right Side View (Medium)
- Vertical Order Traversal of a Binary Tree (Hard)
- Maximum Depth of Binary Tree (Easy)
- Diameter of Binary Tree (Easy)
- Balanced Binary Tree (Easy)
- Invert Binary Tree (Easy)
- Flatten Binary Tree to Linked List (Medium)
- Same Tree (Easy)
- Symmetric Tree (Easy)
- Subtree of Another Tree (Easy)
- Path Sum (Easy)
- Path Sum II (Medium)
- Path Sum III (Medium)
- Count Good Nodes in Binary Tree (Medium)
- Lowest Common Ancestor of a Binary Tree (Medium)
- Kth Ancestor of a Tree Node (Hard)
- All Nodes Distance K in Binary Tree (Medium)
- Validate Binary Search Tree (Medium)
- Search in a Binary Search Tree (Easy)
- Insert into a Binary Search Tree (Medium)
- Delete Node in a BST (Medium)
- Kth Smallest Element in a BST (Medium)
- Binary Search Tree Iterator (Medium)
- Convert Sorted Array to Binary Search Tree (Easy)
- AVL Tree Insert (Hard)
- Construct Binary Tree from Preorder and Inorder Traversal (Medium)
- Serialize and Deserialize Binary Tree (Hard)