-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
110 lines (99 loc) · 3.31 KB
/
mod.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
98
99
100
101
102
103
104
105
106
107
108
109
110
pub 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 replace_value_in_tree(
root: Option<Rc<RefCell<TreeNode>>>,
) -> Option<Rc<RefCell<TreeNode>>> {
let mut vec1 = Vec::new();
let mut vec2 = Vec::new();
let mut vec3 = Vec::new();
vec1.push(root.as_ref().unwrap().clone());
while !vec1.is_empty() {
let mut sum = 0;
for node in vec1.iter() {
let mut t = 0;
if let Some(left) = &node.borrow().left {
t += left.borrow().val;
vec2.push(left.clone());
}
if let Some(right) = &node.borrow().right {
t += right.borrow().val;
vec2.push(right.clone());
}
sum += t;
vec3.push(t);
}
for (node, &t) in vec1.iter().zip(&vec3) {
if let Some(left) = &node.borrow().left {
left.borrow_mut().val = sum - t;
}
if let Some(right) = &node.borrow().right {
right.borrow_mut().val = sum - t;
}
}
std::mem::swap(&mut vec1, &mut vec2);
vec2.clear();
vec3.clear();
}
root.as_ref().unwrap().borrow_mut().val = 0;
root
}
}
#[cfg(test)]
mod tests {
use super::*;
impl TreeNode {
#[inline]
fn new_with(
val: i32,
left: Option<Rc<RefCell<TreeNode>>>,
right: Option<Rc<RefCell<TreeNode>>>,
) -> Option<Rc<RefCell<Self>>> {
Some(Rc::new(RefCell::new(TreeNode { val, left, right })))
}
}
#[test]
fn test1() {
let t1 = Some(Rc::new(RefCell::new(TreeNode::new(1))));
let t2 = Some(Rc::new(RefCell::new(TreeNode::new(10))));
let t3 = TreeNode::new_with(4, t1, t2);
let t4 = Some(Rc::new(RefCell::new(TreeNode::new(7))));
let t5 = TreeNode::new_with(9, None, t4);
let root1 = TreeNode::new_with(5, t3, t5);
let t6 = Some(Rc::new(RefCell::new(TreeNode::new(7))));
let t7 = Some(Rc::new(RefCell::new(TreeNode::new(7))));
let t8 = TreeNode::new_with(0, t6, t7);
let t9 = Some(Rc::new(RefCell::new(TreeNode::new(11))));
let t10 = TreeNode::new_with(0, None, t9);
let root2 = TreeNode::new_with(0, t8, t10);
assert_eq!(Solution::replace_value_in_tree(root1), root2);
}
#[test]
fn test2() {
let t1 = Some(Rc::new(RefCell::new(TreeNode::new(1))));
let t2 = Some(Rc::new(RefCell::new(TreeNode::new(2))));
let root1 = TreeNode::new_with(3, t1, t2);
let t3 = Some(Rc::new(RefCell::new(TreeNode::new(0))));
let t4 = Some(Rc::new(RefCell::new(TreeNode::new(0))));
let root2 = TreeNode::new_with(0, t3, t4);
assert_eq!(Solution::replace_value_in_tree(root1), root2);
}
}