题解 | #二叉树的镜像#

二叉树的镜像

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

/*
 * function TreeNode(x) {
 *   this.val = x;
 *   this.left = null;
 *   this.right = null;
 * }
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param pRoot TreeNode类 
 * @return TreeNode类
 */
    function swap(pRoot){
        if(pRoot == null){
            return null
        }
        swap(pRoot.left);
        swap(pRoot.right)
        // let mid = new TreeNode(pRoot.left.val);
        // console.log("mid",mid);
        let mid = pRoot.left;
        pRoot.left = pRoot.right;
        pRoot.right = mid;
        return pRoot

    }
    function Mirror( pRoot ) {
        // write code here
        // if(pRoot == null){
        //     return null
        // }
        // Mirror(pRoot.left);
        // Mirror(pRoot.right)
        // // let mid = new TreeNode(pRoot.left.val);
        // // console.log("mid",mid);
        // let mid = pRoot.left;
        // pRoot.left = pRoot.right;
        // pRoot.right = mid;
        // return pRoot
        return swap(pRoot)
    }
module.exports = {
    Mirror : Mirror
};

#我的实习求职记录#
全部评论

相关推荐

牛客517626884号:嵌入式真难啊今年,我电赛国二都成了路边野狗了
点赞 评论 收藏
分享
昨天 22:17
北京大学
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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