剑指offer --二叉树的镜像

二叉树的镜像

http://www.nowcoder.com/questionTerminal/564f4c26aa584921bc75623e48ca3011

解法

解法很简单 只要root的左子树 等于 root的右子树,root的右子树 等于左子树就行了 ,用临时结点记录一下就ok

代码

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public void Mirror(TreeNode root) {
        if(root != null ){
            TreeNode right = root.right;
            TreeNode left = root.left;
            root.left = right;
            root.right = left;
            Mirror(root.left);
            Mirror(root.right);
        }

    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务