We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 2902a5d + 40436dc commit f81fb26Copy full SHA for f81fb26
invert-binary-tree/DaleSeo.rs
@@ -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