int maxDown(TreeNode* node) {
if (!node) return 0;
return node->val + max({0, maxDown(node->left), maxDown(node->right)});
}
int maxPathSum(TreeNode* root) {
int best = INT_MIN;
function<void(TreeNode*)> visit = [&](TreeNode* node) {
if (!node) return;
int left = max(maxDown(node->left), 0);
int right = max(maxDown(node->right), 0);
best = max(best, node->val + left + right);
visit(node->left);
visit(node->right);
};
visit(root);
return best;
}
public int maxPathSum(TreeNode root) {
int[] best = {Integer.MIN_VALUE};
visit(root, best);
return best[0];
}
private void visit(TreeNode node, int[] best) {
if (node == null) return;
int left = Math.max(maxDown(node.left), 0);
int right = Math.max(maxDown(node.right), 0);
best[0] = Math.max(best[0], node.val + left + right);
visit(node.left, best);
visit(node.right, best);
}
private int maxDown(TreeNode node) {
if (node == null) return 0;
return node.val + Math.max(0, Math.max(maxDown(node.left), maxDown(node.right)));
}
def max_path_sum(root):
best = float('-inf')
def visit(node):
nonlocal best
if not node: return
left = max(max_down(node.left), 0)
right = max(max_down(node.right), 0)
best = max(best, node.val + left + right)
visit(node.left)
visit(node.right)
visit(root)
return best
def max_down(node):
if not node: return 0
return node.val + max(0, max_down(node.left), max_down(node.right))
function maxPathSum(root: TreeNode | null): number {
let best = -Infinity;
const visit = (node: TreeNode | null): void => {
if (!node) return;
const left = Math.max(maxDown(node.left), 0);
const right = Math.max(maxDown(node.right), 0);
best = Math.max(best, node.val + left + right);
visit(node.left);
visit(node.right);
};
visit(root);
return best;
}
function maxDown(node: TreeNode | null): number {
if (!node) return 0;
return node.val + Math.max(0, maxDown(node.left), maxDown(node.right));
}
int maxPathSum(TreeNode* root) {
int best = INT_MIN;
function<int(TreeNode*)> gain = [&](TreeNode* node) -> int {
if (!node) return 0;
int left = max(gain(node->left), 0);
int right = max(gain(node->right), 0);
best = max(best, node->val + left + right);
return node->val + max(left, right);
};
gain(root);
return best;
}
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(gain(node.left, best), 0);
int right = Math.max(gain(node.right, best), 0);
best[0] = Math.max(best[0], node.val + left + right);
return node.val + Math.max(left, right);
}
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 best
function maxPathSum(root: TreeNode | null): number {
let best = -Infinity;
const gain = (node: TreeNode | null): number => {
if (!node) return 0;
const left = Math.max(gain(node.left), 0);
const right = Math.max(gain(node.right), 0);
best = Math.max(best, node.val + left + right);
return node.val + Math.max(left, right);
};
gain(root);
return best;
}
int maxPathSum(TreeNode* root) {
function<pair<int, int>(TreeNode*)> go = [&](TreeNode* node) -> pair<int, int> {
if (!node) return {INT_MIN, 0};
auto [lb, lg] = go(node->left);
auto [rb, rg] = go(node->right);
int through = node->val + max(lg, 0) + max(rg, 0);
return {max({lb, rb, through}), node->val + max({lg, rg, 0})};
};
return go(root).first;
}
public int maxPathSum(TreeNode root) {
return go(root)[0];
}
private int[] go(TreeNode node) {
if (node == null) return new int[]{Integer.MIN_VALUE, 0};
int[] l = go(node.left); int lb = l[0], lg = l[1];
int[] r = go(node.right); int rb = r[0], rg = r[1];
int through = node.val + Math.max(lg, 0) + Math.max(rg, 0);
return new int[]{Math.max(Math.max(lb, rb), through), node.val + Math.max(Math.max(lg, rg), 0)};
}
def max_path_sum(root):
def go(node):
if not node: return float('-inf'), 0
lb, lg = go(node.left)
rb, rg = go(node.right)
through = node.val + max(lg, 0) + max(rg, 0)
return max(lb, rb, through), node.val + max(lg, rg, 0)
return go(root)[0]
function maxPathSum(root: TreeNode | null): number {
const go = (node: TreeNode | null): [number, number] => {
if (!node) return [-Infinity, 0];
const [lb, lg] = go(node.left);
const [rb, rg] = go(node.right);
const through = node.val + Math.max(lg, 0) + Math.max(rg, 0);
return [Math.max(lb, rb, through), node.val + Math.max(lg, rg, 0)];
};
return go(root)[0];
}
int maxPathSum(TreeNode* root) {
if (!root) return 0;
int best = INT_MIN;
unordered_map<TreeNode*, int> gain;
auto g = [&](TreeNode* n) { return n ? max(gain[n], 0) : 0; };
vector<TreeNode*> stack;
TreeNode* node = root;
TreeNode* last = nullptr;
while (node || !stack.empty()) {
while (node) {
stack.push_back(node);
node = node->left;
}
TreeNode* top = stack.back();
if (top->right && top->right != last) {
node = top->right;
} else {
stack.pop_back();
int left = g(top->left);
int right = g(top->right);
best = max(best, top->val + left + right);
gain[top] = top->val + max(left, right);
last = top;
}
}
return best;
}
public int maxPathSum(TreeNode root) {
if (root == null) return 0;
int best = Integer.MIN_VALUE;
Map<TreeNode, Integer> gain = new HashMap<>();
java.util.function.Function<TreeNode, Integer> g = n -> n == null ? 0 : Math.max(gain.get(n), 0);
Deque<TreeNode> stack = new ArrayDeque<>();
TreeNode node = root;
TreeNode last = null;
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
node = node.left;
}
TreeNode top = stack.peek();
if (top.right != null && top.right != last) {
node = top.right;
} else {
stack.pop();
int left = g.apply(top.left);
int right = g.apply(top.right);
best = Math.max(best, top.val + left + right);
gain.put(top, top.val + Math.max(left, right));
last = top;
}
}
return best;
}
def max_path_sum(root):
if not root: return 0
best = float('-inf')
gain = {}
g = lambda n: max(gain[n], 0) if n else 0
stack = []
node = root
last = None
while node or stack:
while node:
stack.append(node)
node = node.left
top = stack[-1]
if top.right and top.right is not last:
node = top.right
else:
stack.pop()
left = g(top.left)
right = g(top.right)
best = max(best, top.val + left + right)
gain[top] = top.val + max(left, right)
last = top
return best
function maxPathSum(root: TreeNode | null): number {
if (!root) return 0;
let best = -Infinity;
const gain = new Map<TreeNode, number>();
const g = (n: TreeNode | null): number => (n ? Math.max(gain.get(n)!, 0) : 0);
const stack: TreeNode[] = [];
let node: TreeNode | null = root;
let last: TreeNode | null = null;
while (node || stack.length > 0) {
while (node) {
stack.push(node);
node = node.left;
}
const top = stack[stack.length - 1];
if (top.right && top.right !== last) {
node = top.right;
} else {
stack.pop();
const left = g(top.left);
const right = g(top.right);
best = Math.max(best, top.val + left + right);
gain.set(top, top.val + Math.max(left, right));
last = top;
}
}
return best;
}