[Leetcode 100]判断二叉树相同 Same Tree
【题目】
判断二叉树是否相同。
【思路】
check函数。
p==null并且q==null,返回true;(两边完全匹配)
p==null或q==null,返回false;(p、q其中一方更短)
p.val==q.val,值相同,继续迭代向左向右遍历check(p.left,q.left)&&check(p.right,q.right);
【代码】
public boolean check(TreeNode p, TreeNode q){
if(p==null&&q==null)
return true;
if(p==null||q==null)
return false;
if(p.val==q.val)
return check(p.left,q.left)&&check(p.right,q.right);
return false;
}
[Leetcode 100]判断二叉树相同 Same Tree的更多相关文章
- [Leetcode 101]判断对称树 Symmetric Tree
[题目] Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- LeetCode 100. 相同的树(Same Tree) 2
100. 相同的树 100. Same Tree 题目描述 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 每日一算法2019/5 ...
- LeetCode 101. 对称二叉树(Symmetric Tree)
题目描述 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null, ...
- leetcode 98,判断二叉树为BST
方法一,记录子树的上界和下界,root的左子树一定小于root的值,root的右子树一定大于root的值,然后递归左子树和右子树 public class Solution { public bool ...
- 递归 - Leetcode 110 判断二叉树是否为平衡二叉树
110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【easy】110. Balanced Binary Tree判断二叉树是否平衡
判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...
- [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
随机推荐
- 雷林鹏分享:XML 注意事项
XML 注意事项 这里列出了您在使用 XML 时应该尽量避免使用的技术. Internet Explorer - XML 数据岛 它是什么?XML 数据岛是嵌入到 HTML 页面中的 XML 数据. ...
- springboot---->javax.servlet.ServletException
springboot访问静态资源时发生以下异常: javax.servlet.ServletException: Circular view path [login]: would dispatch ...
- Passenger简介
https://www.phusionpassenger.com/docs/tutorials/what_is_passenger/ What is Passenger? 一个开源的web程序服务.它 ...
- SVN的安装
Svn服务器的安装和配置 注意,一定要切换到最高管理权限: su root 通过这个命令就可以完成! 1.安装svn服务器端软件从镜像服务器或者YUM源下载安装SVN服务器软件:yum insta ...
- HDu4794 斐波那契循环节
题意:Arnold变换把矩阵(x,y)变成((x+y)%n,(x+2*y)%n),问最小循环节 题解:仔细算前几项能看出是斐波那契数论modn,然后套个斐波那契循环节板子即可 //#pragma GC ...
- 【JS】【3】标签显示几秒后自动隐藏
$("#XXX").show().delay(2000).hide(0); 2000,0:可选,速度,(毫秒:"slow":"fast") ...
- 解决Maven下载依赖慢的问题(转)
使用Maven构建项目时,项目中有的依赖包可能下载的非常慢,我们可以通过配置镜像来解决这个问题. 之前开源中国的那个好像已经关闭了,于是我找到了一个阿里的来解决. 在Maven的配置文件(%MAVEN ...
- HTTP及RFC解析。
HTTP协议描述的是发送方与接收方的通信协议,通过两方的自觉遵守而存在,当然有不少的浏览器并没有百分百遵守这份协议.HTTP是运行于应用层的协议,基于TCP协议而运作.基本上是客户/服务器对答模式,其 ...
- Nodejs--url模块
由于GET请求直接被嵌入在路径中,URL是完整的请求路径,包括了?后面的部分,因此你可以手动解析后面的内容作为GET请求的参数. url 模块中的 parse 函数可以用于解析url中的参数. url ...
- Web Services的学习二
1.SOAP简单对象访问协议 基于XML的简单协议,可让应用程序在HTTP上进行信息交换,或者说SOAP就是用于访问网络服务的协议.它独立于平台,独立于语言,很简单并可扩展,而且允许绕过防火墙. 2. ...