二叉树的镜像

操作给定的二叉树,将其变换为源二叉树的镜像。

这是第二次遇见这个题目了,直接递归,到底为空时返回

不为空时操作左右孩子节点交换,然后返回,直到根节点

/**
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 ;
        Mirror(root.left);
        Mirror(root.right);
        TreeNode p=root.left;
        root.left=root.right;
        root.right=p;
        return ;
    }
}

posted @ 2019-09-17 15:55  小路不会迷路  阅读(165)  评论(0编辑  收藏  举报