题解 | #二叉树的镜像#

二叉树的镜像

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

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param pRoot TreeNode类 
     * @return TreeNode类
     */
    public TreeNode Mirror (TreeNode pRoot) {
        // write code here

        return process(pRoot);
    }

    public TreeNode process(TreeNode pRoot) {
        // 递归出口
        if (pRoot == null) {
            return null;
        }

        // 得到左右子树调整好的根结点
        TreeNode leftRoot = process(pRoot.left);
        TreeNode rightRoot = process(pRoot.right);

        // 将本根结点的子树也调整好
        pRoot.left = rightRoot;
        pRoot.right = leftRoot;

        // 返回本根结点
        return pRoot;
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
09-19 14:43
实习之后才知道团队氛围的重要性来了一周,从第三天就开始想离职……团子背景、薪资福利再怎么好,也不香了
码农索隆:确实,团队的氛围真的很影响心情,好的团队上班感觉轻松愉快,不好的团队,每天没事就整点幺蛾子
投递美团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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