牛客——二叉树的镜像

ps:今天做一个简单的

题目描述
操作给定的二叉树,将其变换为源二叉树的镜像。
输入描述:
二叉树的镜像定义:源二叉树
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) {
            // write code here
            if (root === null) {
                return null;
            }
            let tep; 
     
            //左右子树互换
            tep = root.left;
            root.left = root.right;
            root.right = tep;
            //转换左子树
            Mirror(root.left);
            // 转换右子树
            Mirror(root.right);
        }

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章