LeetCode 100 及 101题
100. 相同的树
给定两个二叉树,编写一个函数来检验它们是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
示例 1:
输入: 1 1
/ \ / \
2 3 2 3 [1,2,3], [1,2,3] 输出: true
示例 2:
输入: 1 1
/ \
2 2 [1,2], [1,null,2] 输出: false
示例 3:
输入: 1 1
/ \ / \
2 1 1 2 [1,2,1], [1,1,2] 输出: false
源码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null)
return true;
else if(!(p != null && q != null))
return false;
else {
if(p.val == q.val)
return(isSameTree(p.left, q.left) && isSameTree(p.right, q.right));
else
return false;
}
}
}
这道题我的解题思路是通过递归遍历这两个树。当两个树当前的节点对应的值相等时,调用自己的方法判断当前节点的左右两个子树是不是也是相同的树,递归终止的条件就是:如果两个树的当前节点都为空,返回true;一个树为空一个不为空,返回false;当前节点的值不相同,也返回false。

101. 对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/ \
2 2
\ \
3 3
说明:
如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
这道题一看到,我就联想到了上面一题,我们只要从根节点处分开两个子树,同样用递归的方法判断两个子树是不是“相等”,这里只需要把在调用自身方法时的参数改成 “左等于右”&&”右等于左“ 即可,代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null)
return true;
else
return isMirror(root.left, root.right);
}
public boolean isMirror(TreeNode m, TreeNode n) {
if(m == null & n == null)
return true;
else if(m !=null && n !=null && m.val == n.val)
return (isMirror(m.left, n.right) && isMirror(m.right, n.left));
else
return false;
}
}
执行用时 : 1 ms, 在Symmetric Tree的Java提交中击败了99.59% 的用户
内存消耗 : 34.8 MB, 在Symmetric Tree的Java提交中击败了85.06% 的用户
按照题目要求,还可以用循环的方法做这道题。我的思路是,设定两个 List,分别代表从根节点分开的左边的树和右边的树,在设定两个 int 类型变量作为 List 的光标(index),将两个子树的头加入 List 中。循环体中:当两个树对应节点处的值相等,则将左边树的左、右子节点依次加入 List1,对应的将右边树的右、左子节点;依次加入 List2,然后两个光标自加;若对应节点都为空,则光标自加,然后 continue 继续下一次循环;若只有一个为空,返回 false;其他情况也返回 false。循环的终止条件是,光标的值不小于 List 的长度了,也就是任意一个 List 的每个元素已经循环完了。若循环结束,最后此方法返回 true。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;
List<TreeNode> l1 = new ArrayList<TreeNode>();
List<TreeNode> l2 = new ArrayList<TreeNode>();
l1.add(root.left);
l2.add(root.right);
int cur1 = 0;
int cur2 = 0;
while(cur1 < l1.size() && cur2 < l2.size()) {
if(l1.get(cur1) == null && l2.get(cur2) == null) {
cur1++;
cur2++;
continue;
}
else if(l1.get(cur1) == null || l2.get(cur2) == null)
return false;
else if(l1.get(cur1).val == l2.get(cur2).val) {
l1.add(l1.get(cur1).left);
l1.add(l1.get(cur1++).right);
l2.add(l2.get(cur2).right);
l2.add(l2.get(cur2++).left);
}
else
return false;
}
return true;
}
}
执行用时 : 3 ms, 在Symmetric Tree的Java提交中击败了64.39% 的用户
内存消耗 : 34.6 MB, 在Symmetric Tree的Java提交中击败了88.39% 的用户
LeetCode 100 及 101题的更多相关文章
- [LeetCode] 接雨水,题 Trapping Rain Water
这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...
- AI面试必备/深度学习100问1-50题答案解析
AI面试必备/深度学习100问1-50题答案解析 2018年09月04日 15:42:07 刀客123 阅读数 2020更多 分类专栏: 机器学习 转载:https://blog.csdn.net ...
- LeetCode Top 100 Liked 点赞最高的 100 道算法题
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:刷题顺序,刷题路径,好题,top100,怎么刷题,Leet ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
- Leetcode 记录(101~200)
Now, I want to just use English to explain the problem, it's about two month before the interview, s ...
- 每日温度(LeetCode Medium难度算法题)题解
LeetCode 题号739中等难度 每日温度 题目描述: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 ...
- Leetcode按Tag刷题
按照Leetcode的Tag来刷题,从easy到hard刷题 关于如何让Leetcode按难易程度排序,可按以下步骤: 1. 进入Leetcode后,点击code 2.点击code后,可查看所有题目, ...
- LeetCode第十二题-将数字转化为罗马数字
Integer to Roman 问题简介:将输入的int类型数字转化为罗马数字 问题详解:罗马数字由七个不同的符号表示:I,V,X,L,C,D和M 符号-数值 I - 1 V - 5 X -10 L ...
- 将100道计算题输出至txt文件,再读取文件至控制台,在控制台中输入答案并评判对错
我在课堂上基本完成了输出100道题和创建文档,但是因为对输入输出流不熟悉,所以并没有实现将输出的计算题导出到文档里,在课下我又请教了宿舍的大佬,基本完成如下: 源代码: import java.io. ...
随机推荐
- 001 - 配置Pycharm的字体大小
本文记录的是Pycharm2017年1月版本 1 配置代码区的字体大小 位置在 File -> setting -> Editor -> Color&Fonts -> ...
- AutoIt:应用WMI接口,打印远程机器安装的所有应用程序
Dim $computer ="computer name" Dim $Remoteuser ="Login account" Dim $Remotepass ...
- POJ3261(后缀数组+2分枚举)
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 12972 Accepted: 5769 Ca ...
- eclipse编辑窗口不见了(打开左边的java、xml文件,中间不会显示代码)
转自:https://blog.csdn.net/u012062810/article/details/46729779?utm_source=blogxgwz4 1. windows-->re ...
- 【222】◀▶ IDL 输入输出函数说明
参考:I/O - General Input/Output Routines —— 基本输入输出函数 01 PRINT/PRINTF 格式化输出. 02 READ/READF 格式化输入. 0 ...
- Qt传入参数argc argv[]
QtCreator是非常不错的IDE,最近在做的Qt命令行应用,因为调试的环境不同等问题,需要在调试的时候为 main() 传入参数.度娘了半天,没找到方法,只能自力更生.后来在“项目-构建和运行-运 ...
- sql语句之约束条件
not null约束,需设置默认值 sex enum('male','female') not null default 'male' unique 约束,值唯一 单列唯一: create table ...
- 您的Microsof Internet Explorer浏览器包含新版本的内置Adobe Flash Player。
win8 安装adobe flash player时错误提示: 您的Microsof Internet Explorer浏览器包含新版本的内置Adobe Flash Player. 感谢达人提供了如下 ...
- C#下Hashtable和Dictionary之间的差别
Hashtable和Dictionary都是.Net下的表示键值对的集合,那么我们在使用中该选择Hashtable还是Dictionary?下边我们看看他们之间的区别:1.Dictionary< ...
- KING_Unity学习之UGUI_Canvas渲染顺序以及层次关系总结
http://blog.csdn.net/kingsea168/article/details/50252733 之前一直用NGUI开发界面,但看到现在的unity的新版本的UGUI也不错,这几天专门 ...