use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { fn new(val: i32) -> Rc<RefCell<TreeNode>> { Rc::new(RefCell::new(TreeNode { val, left: None, right: None })) } } fn max_path_sum(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { fn helper(node: Option<Rc<RefCell<TreeNode>>>, max_sum: &mut i32) -> i32 { if let Some(n) = node { let left = helper(n.borrow().left.clone(), max_sum).max(0); let right = helper(n.borrow().right.clone(), max_sum).max(0); let current = n.borrow().val + left + right; *max_sum = (*max_sum).max(current); n.borrow().val + left.max(right) } else { 0 } } let mut max_sum = i32::MIN; helper(root, &mut max_sum); max_sum } fn main() { let root = TreeNode::new(1); root.borrow_mut().left = Some(TreeNode::new(2)); root.borrow_mut().right = Some(TreeNode::new(3)); println!("{}", max_path_sum(Some(root))); }
The maximum path sum is 2 + 1 + 3 = 6. The function calculates the maximum sum of any path in the tree, which includes the root and both children.
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { fn new(val: i32) -> Rc<RefCell<TreeNode>> { Rc::new(RefCell::new(TreeNode { val, left: None, right: None })) } } fn max_path_sum(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { fn helper(node: Option<Rc<RefCell<TreeNode>>>, max_sum: &mut i32) -> i32 { if let Some(n) = node { let left = helper(n.borrow().left.clone(), max_sum).max(0); let right = helper(n.borrow().right.clone(), max_sum).max(0); let current = n.borrow().val + left + right; *max_sum = (*max_sum).max(current); n.borrow().val + left.max(right) } else { 0 } } let mut max_sum = i32::MIN; helper(root, &mut max_sum); max_sum } fn main() { let root = TreeNode::new(-10); root.borrow_mut().left = Some(TreeNode::new(9)); root.borrow_mut().right = Some(TreeNode::new(20)); root.borrow_mut().right.as_ref().unwrap().borrow_mut().left = Some(TreeNode::new(15)); root.borrow_mut().right.as_ref().unwrap().borrow_mut().right = Some(TreeNode::new(7)); println!("{}", max_path_sum(Some(root))); }
The maximum path sum is 15 + 20 + 7 = 42. The path does not include the root because it is negative.
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { fn new(val: i32) -> Rc<RefCell<TreeNode>> { Rc::new(RefCell::new(TreeNode { val, left: None, right: None })) } } fn max_path_sum(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { fn helper(node: Option<Rc<RefCell<TreeNode>>>, max_sum: &mut i32) -> i32 { if let Some(n) = node { let left = helper(n.borrow().left.clone(), max_sum); let right = helper(n.borrow().right.clone(), max_sum); let current = n.borrow().val + left + right; *max_sum = (*max_sum).max(current); n.borrow().val + left.max(right) } else { 0 } } let mut max_sum = i32::MIN; helper(root, &mut max_sum); max_sum } fn main() { let root = TreeNode::new(-1); root.borrow_mut().left = Some(TreeNode::new(-3)); println!("{}", max_path_sum(Some(root))); }
The code does not ignore negative sums from children, so it sums -1 + (-3) + 0 = -4 as max path sum. It returns -3, which is incorrect for maximum path sum logic.
For trees with multiple children, the max path through a node can include the node plus the two highest child paths. Summing all children or ignoring the second max path misses some paths.
Using max(0, child_sum) ignores negative sums from child paths, preventing them from lowering the total path sum. This ensures the path sum only increases or stays the same.
