镜像二叉树

题目:操作给定的二叉树,将其变换为原二叉树的镜像。

思路:节点交换即可

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

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

    }

}
*/
 public void Mirror(TreeNode root) {
        if(root==null) return;
        if(root.left==null&&root.right==null) return;
        
        //交换左右节点
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
        
        if(root.left!=null){
              Mirror(root.left);
        }
      
       if(root.right!=null){
             Mirror(root.right);
       }
       
    }

 

posted @ 2017-04-08 15:44  雪浪snowWave  阅读(89)  评论(0编辑  收藏  举报