-
Notifications
You must be signed in to change notification settings - Fork 21
/
recursive.rs
59 lines (43 loc) · 1.5 KB
/
recursive.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
use crate::data_structures::TreeNode;
pub struct Solution;
// ------------------------------------------------------ snip ------------------------------------------------------ //
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
fn helper(node: &RefCell<TreeNode>, result: &mut i32) -> (i32, i32) {
let node = node.borrow();
let mut min = node.val;
let mut max = node.val;
if let Some(left) = node.left.as_deref() {
let (left_min, left_max) = Self::helper(left, result);
min = min.min(left_min);
max = max.max(left_max);
}
if let Some(right) = node.right.as_deref() {
let (right_min, right_max) = Self::helper(right, result);
min = min.min(right_min);
max = max.max(right_max);
}
*result = (*result).max(node.val - min);
*result = (*result).max(max - node.val);
(min, max)
}
pub fn max_ancestor_diff(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
let mut result = 0;
Self::helper(&root.unwrap(), &mut result);
result
}
}
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl super::Solution for Solution {
fn max_ancestor_diff(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
Self::max_ancestor_diff(root)
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}