Skip to content

Commit 218962f

Browse files
authored
invert binary tree solution
1 parent 0b49d70 commit 218962f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

invert-binary-tree/yhkee0404.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
pub fn invert_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
23+
if let Some(u) = &root {
24+
let mut u = u.borrow_mut();
25+
let v = u.left.clone();
26+
u.left = Self::invert_tree(u.right.clone());
27+
u.right = Self::invert_tree(v);
28+
}
29+
root
30+
}
31+
}

0 commit comments

Comments
 (0)