目录
刷题日期: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;
}
}
总结
以上就是本题的内容和学习过程了,递归解法更简单更容易理解一点。
欢迎讨论,共同进步。