forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDaleSeo.rs
More file actions
25 lines (22 loc) · 681 Bytes
/
DaleSeo.rs
File metadata and controls
25 lines (22 loc) · 681 Bytes
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
// 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>>>,
}
use std::rc::Rc;
use std::cell::RefCell;
// TC: O(n)
// SC: O(n)
impl Solution {
pub fn invert_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
if let Some(node) = root.as_ref() {
let left = node.borrow().left.clone();
let right = node.borrow().right.clone();
node.borrow_mut().left = Solution::invert_tree(right);
node.borrow_mut().right = Solution::invert_tree(left);
}
root
}
}