Cover image for Interview Classic 150 Questions P124 Maximum Path Sum in Binary Tree

Interview Classic 150 Questions P124 Maximum Path Sum in Binary Tree


Timeline

timeline

2025-10-28

init

DFS

Title:

Using DFS, the maximum value should be the maximum contribution of the left subtree of a node + the maximum contribution of the right subtree + the value of the current node

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Definition for a binary tree node.
struct Solution;
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}

impl TreeNode {
#[inline]
pub fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None,
}
}
}

use std::cell::RefCell;
use std::cmp::max;
use std::rc::Rc;

impl Solution {
pub fn max_path_sum(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
let mut max_sum = i32::MIN;
Self::dfs(&root, &mut max_sum);
max_sum
}
fn dfs(node: &Option<Rc<RefCell<TreeNode>>>, max_sum: &mut i32) -> i32 {
if let Some(n) = node {
let n = n.borrow();

// Maximum contribution of left and right subtrees (if negative, better not to take)
let left_gain = max(Self::dfs(&n.left, max_sum), 0);
let right_gain = max(Self::dfs(&n.right, max_sum), 0);

// Maximum path sum with current node as the highest point
let current_path_sum = n.val + left_gain + right_gain;

// Update global maximum path sum
*max_sum = max(*max_sum, current_path_sum);

// Return the maximum contribution of the current node to its parent (can only choose one side)
return n.val + max(left_gain, right_gain);
}
0
}
}

fn main() {
println!("Hello, world!");
}

c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <climits>

class Solution {
private:
int maxSum = INT_MIN;

public:
int maxGain(TreeNode* node) {
if (node == nullptr) {
return 0;
}

// Recursively calculate the maximum contribution of left and right child nodes
// Only when the maximum contribution value is greater than 0, the corresponding child node will be selected
int leftGain = std::max(maxGain(node->left), 0);
int rightGain = std::max(maxGain(node->right), 0);

// The maximum path sum of a node depends on the node's value and the maximum contribution of its left and right child nodes
int priceNewpath = node->val + leftGain + rightGain;

// update answer
maxSum = std::max(maxSum, priceNewpath);

// return the maximum contribution value of the node
return node->val + std::max(leftGain, rightGain);
}

int maxPathSum(TreeNode* root) {
maxGain(root);
return maxSum;
}
};

leetcode hot 100 rewrite

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <algorithm>
#include <climits>

class Solution {
private:
int sum;
int __maxPathSum(TreeNode *root)
{
if (root == nullptr)
return 0;
int left_sum = __maxPathSum(root->left);
int right_sum = __maxPathSum(root->right);
sum = std::max({ left_sum + right_sum + root->val, root->val, root->val + left_sum,
root->val + right_sum, sum });

return std::max(std::max(left_sum, right_sum) + root->val, root->val);
}
public:
int maxPathSum(TreeNode *root)
{
sum = INT_MIN;
__maxPathSum(root);
return sum;
}
};