Skip to content

Commit f81fb26

Browse files
authored
Merge pull request #2284 from DaleSeo/main
[DaleSeo] WEEK 10 solutions
2 parents 2902a5d + 40436dc commit f81fb26

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

invert-binary-tree/DaleSeo.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
use std::rc::Rc;
10+
use std::cell::RefCell;
11+
12+
// TC: O(n)
13+
// SC: O(n)
14+
impl Solution {
15+
pub fn invert_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
16+
if let Some(node) = root.as_ref() {
17+
let left = node.borrow().left.clone();
18+
let right = node.borrow().right.clone();
19+
20+
node.borrow_mut().left = Solution::invert_tree(right);
21+
node.borrow_mut().right = Solution::invert_tree(left);
22+
}
23+
root
24+
}
25+
}

0 commit comments

Comments
 (0)