剑指offer JS题解 (18)二叉树的镜像

题目描述

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

解题思路

逐层对节点的左右子树进行交换即可达到目的,这里的交换使用了解构赋值的写法。

Code

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
function Mirror(root)
{
    // write code here
    if(!root) return null;
    [root.left,root.right]=[root.right,root.left];
    Mirror(root.left);
    Mirror(root.right);
    return root;
}

运行环境:JavaScript (V8 6.0.0)
运行时间:23ms
占用内存:9712k

猜你喜欢

转载自blog.csdn.net/qq_40340478/article/details/106171015