【剑指Offer】个人学习笔记_27_二叉树的镜像

刷题日期:21:0103 星期四2021年4月1日

个人刷题记录,代码收集,来源皆为leetcode

经过多方讨论和请教,现在打算往Java方向发力

主要答题语言为Java

题目:

剑指 Offer 27. 二叉树的镜像

难度简单123

请完成一个函数,输入一个二叉树,该函数输出它的镜像。

例如输入:

4 / \ 2 7 / \ / \1 3 6 9
镜像输出:

   4  /  \ 7   2 / \  / \9  6 3  1

示例 1:

输入:root = [4,2,7,1,3,6,9]
输出:[4,7,2,9,6,3,1]

限制:

0 <= 节点个数 <= 1000
题目分析

这道题这么规则,能不能直接从1、2、4、8这样的下标找规律,给相应层的数据倒序就好了。

如果是正常的解法的话,那么肯定还是得递归,需要改变左右子树的索引,返回的也是新子树的根节点。

有了上一道的经验,遍历的问题不大,关键是如何实现在中间过程的镜像操作。

初始解答:

尝试自己解答

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode mirrorTree(TreeNode root) {
        //感觉大佬的子程序都是recur
        //交换左右子树
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        //接着递归子树,同样处理
        mirrorTree(root.left);
        mirrorTree(root.right);
        return root;
    }
}

基本的语法都不过,参考了方法一进行修改,把有些地方想复杂了,整体思路是没有问题的。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode mirrorTree(TreeNode root) {
        if (root == null) return null; //如果空,返回空
        //感觉大佬的子程序都是recur
        //交换左右子树
        //这里直接在交换里带递归程序
        TreeNode tmp = root.left;
        root.left = mirrorTree(root.right);
        root.right = mirrorTree(tmp);
        return root;
    }
}

执行结果:通过

显示详情

执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户

内存消耗:36.1 MB, 在所有 Java 提交中击败了5.32%的用户 空间效率有点低

看了方法二以后,自己的做法就差一点就成功了,只要先处理下面再交换当前的左右子树即可,边界条件也不可丢

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode mirrorTree(TreeNode root) {
        if (root == null) return null; //如果空,返回空
        //感觉大佬的子程序都是recur
        //交换左右子树
        mirrorTree(root.left);
        mirrorTree(root.right);
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        
        return root;
    }
}

学习他人:

方法一:

宝石L1 2020-02-12

Java题解 简单明了

借助个临时节点来避免覆盖后找不到左(右)节点。

    public TreeNode mirrorTree(TreeNode root) {
        
        if (root == null)
            return null;
        
        TreeNode tmpNode = root.left;
        root.left = mirrorTree(root.right);
        root.right = mirrorTree(tmpNode);

        return root;
    }

方法二:

三口一头猪 2020-03-15

菜鸟的第一次双100% 其实和方法一一样,另外建立一次反而多此一举了。

class Solution {
   public TreeNode mirrorTree(TreeNode root) {
        return swap(root);
    }

    TreeNode swap(TreeNode root){
        if(root == null) return root;
        swap(root.left);
        swap(root.right);
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        return root;
    }
}

方法三:

BiemL2 (编辑过)2021-03-06 大佬

一行大法:

C艹

return root == NULL ? NULL : new TreeNode(root->val, mirrorTree(root->right), mirrorTree(root->left));

C#

return root == null ? null : new TreeNode(root.val, MirrorTree(root.right), MirrorTree(root.left));

Java

return root == null ? null : new TreeNode(root.val, mirrorTree(root.right), mirrorTree(root.left));

JavaScript

return root == null ? null : new TreeNode(root.val, mirrorTree(root.right), mirrorTree(root.left));

Python

return TreeNode(root.val, self.mirrorTree(root.right), self.mirrorTree(root.left)) if root else None

方法四:

K神的递归思路差不多,这里是辅助栈的思路,理解就好。

作者:jyd
链接:https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/solution/mian-shi-ti-27-er-cha-shu-de-jing-xiang-di-gui-fu-/
来源:力扣(LeetCode)

class Solution {
    public TreeNode mirrorTree(TreeNode root) {
        if(root == null) return null;
        Stack<TreeNode> stack = new Stack<>() {{ add(root); }};
        while(!stack.isEmpty()) {
            TreeNode node = stack.pop();
            if(node.left != null) stack.add(node.left);
            if(node.right != null) stack.add(node.right);
            TreeNode tmp = node.left;
            node.left = node.right;
            node.right = tmp;
        }
        return root;
    }
}

总结

以上就是本题的内容和学习过程了,递归解法更简单更容易理解一点。

欢迎讨论,共同进步。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值