diff --git a/invert-binary-tree/DaleSeo.rs b/invert-binary-tree/DaleSeo.rs new file mode 100644 index 000000000..a88d1d205 --- /dev/null +++ b/invert-binary-tree/DaleSeo.rs @@ -0,0 +1,25 @@ +// Definition for a binary tree node. +#[derive(Debug, PartialEq, Eq)] +pub struct TreeNode { + pub val: i32, + pub left: Option>>, + pub right: Option>>, +} + +use std::rc::Rc; +use std::cell::RefCell; + +// TC: O(n) +// SC: O(n) +impl Solution { + pub fn invert_tree(root: Option>>) -> Option>> { + 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 + } +}