- Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha0226_invert_binary_tree.rs
139 lines (121 loc) · 4.26 KB
/
a0226_invert_binary_tree.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* [0226] invert-binary-tree
*/
usesuper::utils::tree::*;
pubstructSolution{}
// solution impl starts here
// 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;
implSolution{
// 使用clone
pubfninvert_tree(root:Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>>{
ifletSome(root) = root {
letmut root_mut = root.borrow_mut();
let temp = root_mut.left.clone();
root_mut.left = root_mut.right.clone();
root_mut.right = temp;
Self::invert_tree(root_mut.left.clone());
Self::invert_tree(root_mut.right.clone());
drop(root_mut);
returnSome(root);
}
Option::None
}
// 使用unsafe
pubfninvert_tree_2(mutroot:Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>>{
if root.is_none(){
returnNone;
}
let ref_mut = root.as_mut().unwrap().as_ptr();
unsafe{
let l = &mut(*ref_mut).left;
let r = &mut(*ref_mut).right;
std::mem::swap(l, r);
if l.is_some(){
Self::invert_tree(Some(l.as_mut().unwrap().clone()));
}
if r.is_some(){
Self::invert_tree(Some(r.as_mut().unwrap().clone()));
}
}
root
}
// 使用take
pubfninvert_tree_3(root:Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>>{
if root.is_none(){
returnNone;
}
let root = root.unwrap();
let l = Self::invert_tree(root.borrow_mut().left.take());
let r = Self::invert_tree(root.borrow_mut().right.take());
root.borrow_mut().left = r;
root.borrow_mut().right = l;
Some(root)
}
}
// solution impl ends here
// solution tests starts here
#[cfg(test)]
mod tests {
usesuper::*;
use test::Bencher;
#[test]
fntest_case0(){
assert_eq!(
Solution::invert_tree(tree![4,2,7,1,3,6,9]),
tree![4,7,2,9,6,3,1]
);
}
#[bench]
fnbench_invert_tree(b:&mutBencher){
let t = tree![
4,1,4,6,8,9,5,3,1,3,5,6,7,8,9,7,65,3,2,2,3,4,45,6,6,78,9,0,
8,7,6,67,8,9,9,7,5654,43,3,2,1,1,2,3,4,4,45,5,6,6,7,78,8,76,
5,4,67,8,9,90,009,7,65,87,654,356,789,87,6543,4,567,89,765,4,567,
89,87,654,35,6789,0,876,543,4,5678,90,876,5432,1,345,67890,9876,54321,
2,345,6789,87,65432,1,234,5678,2,7,1,3,6,9
];
b.iter(|| Solution::invert_tree(t.clone()));
}
#[bench]
fnbench_invert_tree_2(b:&mutBencher){
let t = tree![
4,1,4,6,8,9,5,3,1,3,5,6,7,8,9,7,65,3,2,2,3,4,45,6,6,78,9,0,
8,7,6,67,8,9,9,7,5654,43,3,2,1,1,2,3,4,4,45,5,6,6,7,78,8,76,
5,4,67,8,9,90,009,7,65,87,654,356,789,87,6543,4,567,89,765,4,567,
89,87,654,35,6789,0,876,543,4,5678,90,876,5432,1,345,67890,9876,54321,
2,345,6789,87,65432,1,234,5678,2,7,1,3,6,9
];
b.iter(|| Solution::invert_tree_2(t.clone()));
}
#[bench]
fnbench_invert_tree_3(b:&mutBencher){
let t = tree![
4,1,4,6,8,9,5,3,1,3,5,6,7,8,9,7,65,3,2,2,3,4,45,6,6,78,9,0,
8,7,6,67,8,9,9,7,5654,43,3,2,1,1,2,3,4,4,45,5,6,6,7,78,8,76,
5,4,67,8,9,90,009,7,65,87,654,356,789,87,6543,4,567,89,765,4,567,
89,87,654,35,6789,0,876,543,4,5678,90,876,5432,1,345,67890,9876,54321,
2,345,6789,87,65432,1,234,5678,2,7,1,3,6,9
];
b.iter(|| Solution::invert_tree_3(t.clone()));
}
}
// solution tests ends here