[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 ...
随机推荐
- what are stop words
what are stop words 一.总结 一句话总结:就是在seo的关键词中不要有stop words,不然的话搜索引擎会直接忽略 stop words most common words ...
- cookie知识点
1.springmvc框架中,cookie例子 jsp: <%-- Created by IntelliJ IDEA. User: 44262 Date: 2019/2/28 Time: 18: ...
- phalcon的save方法保存失败?
phalcon的save方法保存失败? 因为表中设置了一个字段:disabled, 默认值是1, 在创建数据的时候,disabled没有传值过去,导致save方法一直是false, 当返回false时 ...
- computational biology | Bioinformatician | 开发者指南
对自己的定位要明确,不要定义为码农,我是computational biologist. 入了这一行就不要三心二意,这基本注定你未来10年都在干这个,就算要转行也要先把这个做好.其实大多数人最喜欢的肯 ...
- ROC曲线(receiver-operating-characteristic curve)-阈值评价标准(转)
转自:http://blog.csdn.net/abcjennifer/article/details/7359370 ROC曲线指受试者工作特征曲线 / 接收器操作特性曲线(receiver ope ...
- wpf 使用DocumentViewer打印
https://blog.csdn.net/duanzi_peng/article/details/14118937?locationNum=15 https://www.cnblogs.com/zl ...
- mandatory and advisory文件锁(File Lock)
http://blog.csdn.net/elfprincexu/article/details/43564425 文件锁(File Lock)是一种在特定的时间内只允许一个进程进行访问文件的机制,通 ...
- JavaScript动态加载资源
//动态加载样式 function dynamicLoadingCss(path){ if(!path || path.length === 0){ return false; } var head ...
- 微信小程序地图demo完整
<block wx:for="{{data_2}}" wx:key='index' wx:if="{{data_2.length}}"> <v ...
- Card Game Again CodeForces - 818E (双指针)
大意: 给定序列, 求多少个区间积被k整除. 整除信息满足单调性, 显然双指针. 具体实现只需要考虑k的素数向量, 对每一维维护个指针即可. 这题看了下cf其他人的做法, 发现可以直接暴力, 若当前的 ...