[LeetCode]题100:Same Tree
Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
即为判断两颗二叉树是否相同。输入是用数组表示的二叉树。

对于图中的树,它的编号是这样的:A(1)B(2)C(3)D(4)E(5)F(6)G(7)
方法一:DFS
# DFS 递归循环
# 判断p或者q是否为空
if p == q == None:
return True
if p==None or q==None:
return False
if p.val!=q.val:
return False
else:
return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
方法二:堆栈
# stack 先进后出
stack = [(p,q)]
while stack:
n1,n2=stack.pop()
if not n1 and not n2:
continue
if not n1 or not n2:
return n1==n2
if n1.val!=n2.val:
return False
stack.append((n1.right,n2.right))
stack.append((n1.left,n2.left))
return True
每一次pop先取left的值。其中一开始忽略了n1和n2都为空的情况,此时不能直接返回true,因为只代表此次两个子节点的值为null不代表两个二叉树为空,需要continue跳出循环执行下一次pop,再取两个节点的值进行比较。
示例:

[LeetCode]题100:Same Tree的更多相关文章
- 【一天一道LeetCode】#100. Same Tree(100题大关)
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- LeetCode之100. Same Tree
------------------------------------------ 递归比较即可 AC代码: /** * Definition for a binary tree node. * p ...
- 【LeetCode】100. Same Tree 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- 【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 OJ 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 (2 solutions)
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- Leetcode题 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- LeetCode Top 100 Liked 点赞最高的 100 道算法题
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:刷题顺序,刷题路径,好题,top100,怎么刷题,Leet ...
随机推荐
- dyld_shared_cache_extract_dylibs failed
文章来源:https://www.jianshu.com/p/e276a784fbee s.png 数据线插上手机 Xcode->window 3.png 2.png 点击Unpai ...
- vue-cli 搭建的项目关闭 eslint
一般不会关闭eslint,基于接手的代码用eslint的时候报错太多,强迫症的人实在忍受不了报错,先实行关闭: 1.在build 下面的 webpack.base.conf.js 找到 module- ...
- 深入理解Java虚拟机6-chap8-9-斗者3星
一.虚拟机字节码执行引擎 1.虚拟机执行引擎由自己实现,所以可以自行制定指令集与执行引擎的体系结构,并且可以执行那些不被硬件直接支持的指令集格式. 2.执行引擎 编译执行:通过JIT编译器产生本地代码 ...
- 深入理解Java虚拟机4-chap6-斗者1星
一.JVM语言无关性 1.以字节码为基础(Class文件为一组以8位字节为基础单位的二进制流),JVM与Class文件关联,而非与Java语言关联 2.代码编译从本地码(Native Code)转为字 ...
- HDU 2874 Connections between cities(LCA Tarjan)
Connections between cities [题目链接]Connections between cities [题目类型]LCA Tarjan &题意: 输入一个森林,总节点不超过N ...
- windows程序设计 vs2012 新建win32项目
1.文件->新建->项目 2.选择win32项目,确定 3.下一步 4.选择windows应用程序,选中空项目,安全开发生命周期不选.点击完成. 5.空项目建好了.
- [Android] macOS的Android Studio快捷键
- 快速输入serialVersionUID - - 设置,左上角,Android Studio -> Preferences -> 搜索“Serializable class witho ...
- RocketMQ使用笔记
apache rocketmq document : http://rocketmq.apache.org/community/ rocketmq 工具下载地址:https://github.com/ ...
- ASP.NET MVC案例教程(三)
ASP.NET MVC案例教程(二) 让第一个页面跑起来 现在,我们来实现公告系统中的第一个页面——首页.它非常简单,只包括所有公告分类的列表,并且每个列表项是一个超链接.其中分类数据是用我们的Moc ...
- zw量化交易·实盘操作·系列培训班
参见: <zw量化交易·实盘操作·系列培训班> http://blog.sina.com.cn/s/blog_7100d4220102w0q5.html