[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 ...
随机推荐
- redis发布订阅、事务、脚本
Redis 发布订阅 Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. Redis 客户端可以订阅任意数量的频道. 下图展示了频道 cha ...
- BMC ipmitool 对linux服务器进行IPMI管理
IPMI是智能型平台管理接口(Intelligent Platform Management Interface)的缩写,是管理基于 Intel结构的企业系统中所使用的外围设备采用的一种工业标准,该标 ...
- 绿色版mssql
1.安装2008绿色版,缺少对应的企业管理器,安装官方版本的提示电脑没有重启(已经重启后) 2.选择一个可用版本的mssql,2000的可以用,MSSQL2000-HaoSQL,自带企业管理器和查询器
- [js]jquery里的jsonp实现ajax异源请求
同源请求-jquery <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/ ...
- eclipse出现An internal error occurred during: "Building workspace". Java heap space 错误
出现这个错误,eclipse 会卡死,以及自动退出 解决方案 工程根目录 找到项目中.project文件 删除这两处 第一处: <buildCommand> <name>org ...
- 2019年度【计算机视觉&机器学习&人工智能】国际重要会议汇总
简介 每年全世界都会举办很多计算机视觉(Computer Vision,CV). 机器学习(Machine Learning,ML).人工智能(Artificial Intelligence ,AI) ...
- BN多卡同步进行
为什么不进行多卡同步? BatchNorm的实现都是只考虑了single gpu.也就是说BN使用的均值和标准差是单个gpu算的,相当于缩小了mini-batch size.至于为什么这样实现,1)因 ...
- redis示例 - 限速器,计时器
INCR INCR key 将 key 中储存的数字值增一. 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作. 如果值包含错误的类型,或字符串类型的值不能表示 ...
- HDU 2586 How far away(dfs+邻接表)
How far away [题目链接]How far away [题目类型]dfs+邻接表 &题意: 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起来,接下了有m次询问,每次询问 ...
- int 的重载
测试代码: 结果: 分析: 首先创建两个对象同时进行初始化所以两次调用带参的构造函数: 其次在创建一个 对象然后将其等于前两个对象相加,这里由于该类没有重载+运算符而是重载了int 所以当两个对象相加 ...