Skip to content

Commit

Permalink
impl invert binary tree
Browse files Browse the repository at this point in the history
  • Loading branch information
SKTT1Ryze committed Apr 13, 2024
1 parent 8a5d6b5 commit a03d24b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions leetcode-rs/src/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl From<String> for TreeNodeHandle {

let nodes: Vec<&str> = value.split(',').collect();

if nodes.is_empty() {
if nodes.is_empty() || nodes[0].is_empty() {
Self { inner: None }
} else {
let mut queue = VecDeque::<Rc<RefCell<TreeNode>>>::new();
Expand Down Expand Up @@ -62,7 +62,7 @@ impl TreeNodeHandle {
pub fn cmp(&self, other: &Self) -> bool {
if self.inner.is_some() && other.inner.is_some() {
let self_ = self.inner.as_ref().unwrap().borrow();
let other_ = self.inner.as_ref().unwrap().borrow();
let other_ = other.inner.as_ref().unwrap().borrow();

self_.val == other_.val
&& Self::new(self_.left.clone())
Expand Down
13 changes: 12 additions & 1 deletion leetcode-rs/src/solutions/invert_binary_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ impl SolutionImpl {
pub fn invert_tree(
root: Option<Rc<RefCell<TreeNode>>>,
) -> Option<Rc<RefCell<TreeNode>>> {
todo!()
if let Some(node) = root {
let mut new_root = TreeNode::new(node.borrow().val);

let new_left = Self::invert_tree(node.borrow().right.clone());
new_root.left = new_left;
let new_right = Self::invert_tree(node.borrow().left.clone());
new_root.right = new_right;

Some(Rc::new(RefCell::new(new_root)))
} else {
None
}
}
}

0 comments on commit a03d24b

Please sign in to comment.