
g0201_0300.s0226_invert_binary_tree.Solution.rs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
// #Easy #Top_100_Liked_Questions #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
// #Data_Structure_I_Day_12_Tree #Level_2_Day_6_Tree #Udemy_Tree_Stack_Queue
// #Big_O_Time_O(n)_Space_O(n) #2024_09_11_Time_0_ms_(100.00%)_Space_2_MB_(77.66%)
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option>>,
// pub right: Option>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn invert_tree(root: Option>>) -> Option>> {
root.map(|node| {
let node = node.borrow();
let mut new_node = TreeNode::new(node.val);
new_node.left = Self::invert_tree(node.right.clone());
new_node.right = Self::invert_tree(node.left.clone());
Rc::new(RefCell::new(new_node))
})
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy