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 ...
随机推荐
- ecshop 默认图处理
function get_banner_path($img) { $img = empty($img) ? C('no_picture') : $img; return $img;}
- xss漏洞挖掘小结
xss漏洞挖掘小结 最近,在挖掘xss的漏洞,感觉xss真的不是想象的那样简单,难怪会成为一类漏洞,我们从防的角度来讲讲xss漏洞的挖掘方法: 1.过滤 一般服务器端都是采用这种方式来防御xss攻击, ...
- C-全局变量与局部变量
- ASP.NET MVC使用Bootstrap系列(5)——创建ASP.NET MVC Bootstrap Helpers
阅读目录 序言 内置的HTML Helpers 创建自定义的Helpers 使用静态方法创建Helpers 使用扩展方法创建Helpers 创建Fluent Helpers 创建自动闭合的Helper ...
- 网络方案 & HTTP状态码
在iOS中,常见的发送HTTP请求的方案包括: 苹果官方 名称 说明 NSURLConnection iOS 2.0 推出,用法简单,最古老最经典最直接的一种方案 NSURLSession iOS 7 ...
- recording just for inquiry in the future
auditd审计 相关命令有: auditd, auditctl, ausearch, aureport 相关文件: /etc/audit/auditd.conf, /etc/audit/audit. ...
- 单例模式singleton
在进行开发的时候,我们在有些情形下有些对象我们只需要一个.例如:配置文件.工具类.线程池.缓存.日志对象等. 如何保证我们的对象只有一个呢?我们可以通过单例来实现. 常用的单例有两种:饿汉模式和懒汉模 ...
- 使用jQuery的Scrollify插件实现鼠标滚轮或者手势滑动到页面下一节点部分
有时我们需要做一个单页面介绍产品特性,而单页面内容非常多且页面非常长,为了快速定位到产品特性节点,我们使用js侦听用户滚轮事件,当用户触发滚轮滑动或者使用手势触屏滑动时,即可定位到相应的节点.一款jQ ...
- jquery eval解析JSON中的注意点介绍
在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式:使用eval()函数.使用Function对象来进行返回解析,下面有个示例,感兴趣的朋友可以参考下 在JS中将JSON的字符串解析 ...
- ApacheServer-----关于443端口被占用的解决方法
最经公司项目需要经过Apache服务器转发,自己也下载了ApacheServer,但是在启动的过程中,遇到443端口被占用,网上看了一些解决方法,都不对,没有解决问题. 执行启动命令httpd -k ...