-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbinary-tree-maximum-path-sum.rs
97 lines (80 loc) · 2.21 KB
/
binary-tree-maximum-path-sum.rs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
// Definition for a binary tree node.
#[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::rc::Rc;
impl Solution {
/// 递归
pub fn max_path_sum(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
if root.is_none() {
return 0;
}
let mut max = None;
Self::max(root, &mut max);
max.unwrap_or_default()
}
fn max(root: Option<Rc<RefCell<TreeNode>>>, global_max: &mut Option<i32>) -> i32 {
if root.is_none() {
return 0;
}
let root = root.unwrap();
let current = root.borrow().val;
let left = root.borrow_mut().left.take();
let right = root.borrow_mut().right.take();
let left_max = Solution::max(left, global_max);
let right_max = Solution::max(right, global_max);
global_max.insert_if(current + left_max.max(0) + right_max.max(0), |x, y| *x > *y);
current + left_max.max(right_max).max(0) // 当前节点的最大和 = 当前节点+子节点的最大值(且此值必须为正数)
}
}
trait InsertIf<T> {
fn insert_if<F>(&mut self, value: T, f: F)
where
F: Fn(&T, &T) -> bool;
}
impl<T: PartialEq + Eq> InsertIf<T> for Option<T> {
fn insert_if<F>(&mut self, value: T, f: F)
where
F: Fn(&T, &T) -> bool,
{
match self {
Some(x) => {
if f(&value, x) {
*self = Some(value);
};
}
None => *self = Some(value),
}
}
}
#[cfg(test)]
mod test {
use crate::InsertIf;
#[test]
fn insert_if() {
let mut n = None;
n.insert_if(10, |x, y| *x > *y);
assert_eq!(n, Some(10));
n.insert_if(11, |x, y| *x > *y);
assert_eq!(n, Some(11));
n.insert_if(9, |x, y| *x > *y);
assert_eq!(n, Some(11));
}
}