leetcode/src/problem/p226_invert_binary_tree.rs
2024-10-24 09:08:13 +08:00

58 lines
1.2 KiB
Rust

/**
* [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<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// 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<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
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]);
}
}