124. Binary Tree Maximum Path Sum

Given the root of a binary tree, return the maximum path sum.

For this problem, a path is defined as any node sequence from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.

 

Example 1:

Input: root = [1,2,3]
Output: 6

Example 2:

Input: root = [-10,9,20,null,null,15,7]
Output: 42

 

Constraints:

  • The number of nodes in the tree is in the range [1, 3 * 104].
  • -1000 <= Node.val <= 1000

Rust Solution

struct Solution;
use rustgym_util::*;

trait Postorder {
    fn postorder(&self, max: &mut i32) -> Option<i32>;
}

impl Postorder for TreeLink {
    fn postorder(&self, max: &mut i32) -> Option<i32> {
        if let Some(node) = self {
            let node = node.borrow();
            let val = node.val;
            let left = &node.left;
            let right = &node.right;
            let left_max = left.postorder(max);
            let right_max = right.postorder(max);
            match (left_max, right_max) {
                (Some(left_max), Some(right_max)) => {
                    *max = (*max).max(val);
                    *max = (*max).max(val + left_max);
                    *max = (*max).max(val + right_max);
                    *max = (*max).max(val + left_max + right_max);
                    Some(val + 0.max(left_max.max(right_max)))
                }
                (Some(left_max), None) => {
                    *max = (*max).max(val);
                    *max = (*max).max(val + left_max);
                    Some(val + 0.max(left_max))
                }
                (None, Some(right_max)) => {
                    *max = (*max).max(val);
                    *max = (*max).max(val + right_max);
                    Some(val + 0.max(right_max))
                }
                (None, None) => {
                    *max = (*max).max(val);
                    Some(val)
                }
            }
        } else {
            None
        }
    }
}

impl Solution {
    fn max_path_sum(root: TreeLink) -> i32 {
        let mut res: i32 = std::i32::MIN;
        root.postorder(&mut res);
        res as i32
    }
}

#[test]
fn test() {
    let root = tree!(1, tree!(2), tree!(3));
    let res = 6;
    assert_eq!(Solution::max_path_sum(root), res);
    let root = tree!(-10, tree!(9), tree!(20, tree!(15), tree!(7)));
    let res = 42;
    assert_eq!(Solution::max_path_sum(root), res);
}

Having problems with this solution? Click here to submit an issue on github.