二叉树的镜像

题目描述

操作给定的二叉树,将其变换为源二叉树的镜像。
 
思路:
  思路很明了,设置一个新结点,左右孩子交换,递归下去。
 不需要想太多,即使左子树为空或右子树为空,只不过是将左子树的null 和右子树进行了交换而已,若当前结点为空,则return 结束该次递归。
/**
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) return;  
        TreeNode tmp = null;
        tmp = root.right;
        root.right = root.left;
        root.left = tmp;
        Mirror(root.left);
        Mirror(root.right);
    }
}

 

posted @ 2018-08-08 09:21  Octopus22  阅读(75)  评论(0编辑  收藏  举报