镜像(反转)二叉树

操作给定的二叉树,将其变换为源二叉树的镜像。思路就是调换左右子树,然后使用递归。

镜像(反转)二叉树_第1张图片

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
function Mirror(root) {
    if(root === null) {
        return;
    }
    var temp = root.left;
    root.left = root.right;
    root.right = temp;
    Mirror(root.left);
    Mirror(root.right);
}

 

你可能感兴趣的:(镜像(反转)二叉树)