二叉树的镜像

2019-11-13  本文已影响0人  ElricTang

《剑指offer》刷题笔记。如有更好解法,欢迎留言。

关键字: 递归

题目描述:

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

二叉树的镜像定义:源二叉树 
            8
           /  \
          6   10
         / \  / \
        5  7 9 11
        镜像二叉树
            8
           /  \
          10   6
         / \  / \
        11 9 7  5

思路:

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
function Mirror(root)
{
    function Left2Right(node){
        if(node !== null){
            [node.left,node.right] = [node.right,node.left];
            node.left && Left2Right(node.left);
            node.right && Left2Right(node.right);
        }
    }
    Left2Right(root);
    return root;
}
上一篇 下一篇

猜你喜欢

热点阅读