题解 | #二叉树的镜像# | Rust

二叉树的镜像

https://www.nowcoder.com/practice/a9d0ecbacef9410ca97463e4a5c83be7

/**
 * #[derive(PartialEq, Eq, Debug, Clone)]
 * pub struct TreeNode {
 *     pub val: i32,
 *     pub left: Option<Box<TreeNode>>,
 *     pub right: Option<Box<TreeNode>>,
 * }
 *
 * impl TreeNode {
 *     #[inline]
 *     fn new(val: i32) -> Self {
 *         TreeNode {
 *             val: val,
 *             left: None,
 *             right: None,
 *         }
 *     }
 * }
 */
struct Solution{

}

impl Solution {
    fn new() -> Self {
        Solution{}
    }

    /**
    * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
    *
    * 
        * @param pRoot TreeNode类 
        * @return TreeNode类
    */
    pub fn Mirror(&self, pRoot: Option<Box<TreeNode>>) -> Option<Box<TreeNode>> {
        let mut pRoot = pRoot;
        if !pRoot.is_none() {
            let (l ,r) = (pRoot.as_mut().unwrap().left.take(), pRoot.as_mut().unwrap().right.take());
            pRoot.as_mut().unwrap().left = Solution::Mirror(self, r);
            pRoot.as_mut().unwrap().right = Solution::Mirror(self, l);

        }
        return pRoot;
    }
}

全部评论

相关推荐

牛客928043833号:在他心里你已经是他的员工了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务