/** * [226] Invert Binary Tree */ pub struct Solution {} use crate::util::tree::{to_tree, TreeNode}; // submission codes start here // 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::cell::RefCell; use std::rc::Rc; impl Solution { pub fn invert_tree(root: Option>>) -> Option>> { match root { None => None, Some(root) => { let left = Self::invert_tree(root.borrow_mut().left.take()); let right = Self::invert_tree(root.borrow_mut().right.take()); root.borrow_mut().left = right; root.borrow_mut().right = left; Some(root) } } } } // submission codes end #[cfg(test)] mod tests { use super::*; #[test] fn test_226() { Solution::invert_tree(tree![2, 1, 3]); } }