Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit adec5c2

Browse files
author
guangsheng.li01
committed
Solved 0226
1 parent 05950c7 commit adec5c2

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

‎src/a0226_invert_binary_tree.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* [0226] invert-binary-tree
3+
*/
4+
5+
use super::utils::tree::*;
6+
pub struct Solution {}
7+
8+
// solution impl starts here
9+
10+
// Definition for a binary tree node.
11+
// #[derive(Debug, PartialEq, Eq)]
12+
// pub struct TreeNode {
13+
// pub val: i32,
14+
// pub left: Option<Rc<RefCell<TreeNode>>>,
15+
// pub right: Option<Rc<RefCell<TreeNode>>>,
16+
// }
17+
//
18+
// impl TreeNode {
19+
// #[inline]
20+
// pub fn new(val: i32) -> Self {
21+
// TreeNode {
22+
// val,
23+
// left: None,
24+
// right: None
25+
// }
26+
// }
27+
// }
28+
use std::cell::RefCell;
29+
use std::rc::Rc;
30+
impl Solution {
31+
pub fn invert_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
32+
if let Some(root) = root {
33+
let mut root_mut = root.borrow_mut();
34+
35+
let temp = root_mut.left.clone();
36+
root_mut.left = root_mut.right.clone();
37+
root_mut.right = temp;
38+
39+
Self::invert_tree(root_mut.left.clone());
40+
Self::invert_tree(root_mut.right.clone());
41+
42+
drop(root_mut);
43+
return Some(root);
44+
}
45+
Option::None
46+
}
47+
}
48+
49+
// solution impl ends here
50+
51+
// solution tests starts here
52+
53+
#[cfg(test)]
54+
mod tests {
55+
use super::*;
56+
57+
#[test]
58+
fn test_case0() {
59+
assert_eq!(
60+
Solution::invert_tree(tree![4, 2, 7, 1, 3, 6, 9]),
61+
tree![4, 7, 2, 9, 6, 3, 1]
62+
);
63+
}
64+
}
65+
66+
// solution tests ends here

‎src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod a0119_pascals_triangle_ii;
2222
mod a0145_binary_tree_postorder_traversal;
2323
mod a0169_majority_element;
2424
mod a0172_factorial_trailing_zeroes;
25+
mod a0226_invert_binary_tree;
2526
mod a0322_coin_change;
2627
mod a0343_integer_break;
2728
mod a0392_is_subsequence;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /