leetcode 100. Same Tree
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
给出两个二叉树,写一个方法判断两个二叉树是否相等,
如果两个二叉树相等,说明结构相同并且每个节点的值相同。
如果两个节点都为空,则说明相同,返回true,
判断两个根节点的值不同,或者一个为空一个不为空,说明两个树不相同,返回false。
然后再递归左右节点,如果有一个为false或者两个都为false,返回false。
时间O(N), 空间O(h)。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null) {
return false;
}
if (p.val != q.val) {
return false;
}
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
leetcode 100. Same Tree的更多相关文章
- LeetCode 100. Same Tree (判断树是否完全相同)
100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
- LeetCode 100. Same Tree (相同的树)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [LeetCode] 100. Same Tree 相同树
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- leetcode 100 Same Tree ----- java
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- Java [Leetcode 100]Same Tree
题目描述: Given two binary trees, write a function to check if they are equal or not. Two binary trees a ...
- [Leetcode]100. Same Tree -David_Lin
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- (二叉树 递归 DFS) leetcode 100. Same Tree
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- [LeetCode] 100. Same Tree ☆(两个二叉树是否相同)
描述 解析 根与根比较,左右子树互相递归比较即可. 代码 /** * Definition for a binary tree node. * public class TreeNode { * in ...
随机推荐
- 10月21日下午PHP常用函数
函数四要素:返回类型 函数名 参数列表 函数体 //最简单的函数定义方式 function Show() { echo "hello"; } Show();//输出结果为he ...
- Java数据结构——用双端链表实现队列
//================================================= // File Name : LinkQueue_demo //---------------- ...
- docker管理shipyard中文版v3.0.2更新
shipyard中文版v3.0.2更新 https://console.dockerclub.net/ https://dockerclub.net/docs/intro/getting_start ...
- Autofac.Integration.Owin
public static IAppBuilder UseAutofacMiddleware(this IAppBuilder app, ILifetimeScope container) { if ...
- Senparc.Weixin.MP.Sample 配置redis服务器密码
redis.windows-service.conf中加 requirepass 你的密码 <!-- Cache.Redis连接配置 --> <add key="Cach ...
- cookie, localStorage, sessionStorage区别
cookie 有过期时间,默认是关闭浏览器后失效,4K,兼容ie6,不可跨域,子域名会继承父域名的cookielocalStorage 永不过期,除非手动删除,5M,兼容IE8,不可跨域,子域名不能继 ...
- [C#]通用守护进程服务
摘要 很多情况下,都会使用windows服务做一些任务,但总会有一些异常,导致服务停止.这个时候,开发人员又不能立马解决问题,所以做一个守护者服务还是很有必要的.当检测到服务停止了,重启一下服务,等开 ...
- vim配色方案设置(更换vim配色方案)
vim配色后,我的 设定底色为黑色,字体为绿色,然后将文件夹设为洋红,默认的注释换为淡黄:其实有一种简单的方法,就是设定为系统配置好的配色方案:转载文章如下: ---------------- ( ...
- 降维PCA技术
降维技术使得数据变得更易使用,并且它们往往能够去除数据中的噪声,使得机器学习任务往往更加精确. 降维往往作为预处理步骤,在数据应用到其它算法之前清洗数据.有很多技术可以用于数据降维,在这些技术中,独立 ...
- c# List AddRange
https://msdn.microsoft.com/zh-cn/library/z883w3dc(v=vs.80).aspx List 中会保留集合中元素的顺序. 如果新的 Count(当前 Cou ...