[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 ...
随机推荐
- 最全的MonkeyRunner自动化测试从入门到精通(3)
一.eclipse的下载安装与配置成安卓开发环境 步骤一:在官网上面进行下载eclipse,官网的网址:https://www.eclipse.org/downloads/ 步骤二:下载完成后可以在你 ...
- sass基础学习(一)
移动端布局各种问题 pc端布局各种问题sass 组件模块化面向对象编程ajax 框架学习 webpack 打包 性能优化 gulp是基于Nodejs的自动任务运行器她能自动化地完成 javascrip ...
- 11.0-uC/OS-III就绪列表(优先级)
准备运行的任务被放置于就绪列表中.就绪列表包括2个部分:位映像组包含了优先级信息,一个表包含了所有指向就绪任务的指针. 1.优先级 图6-1到6-3显示了优先级的位映像组.它的宽度取决于CPU_DAT ...
- PHP 二位数组按照下标排序
1.排序得内容 array(6) { [0] => array(12) { [0] => string(3) "160" [1] => string(2) &qu ...
- python 控制语句基础---->代码块:以为冒号作为开始,用缩进来划分作用域,代表一个整体,是一个代码块,一个文件(模块)也称为一个代码块 | 作用域:作用的范围
# ### 代码块:以为冒号作为开始,用缩进来划分作用域,代表一个整体,是一个代码块,一个文件(模块)也称为一个代码块 # ### 作用域:作用的范围 print(11) print(12) prin ...
- Maven 编译跳过检查
Maven 编译跳过检查 使用maven打包的时候指令:clean package, 但过程可会有提示检测错误,如果想跳过检查,需加上:-Dmaven.test.skip=true 完整指令: cle ...
- CentOS 7 搭建CA认证中心实现https取证
CA认证中心简述 CA :CertificateAuthority的缩写,通常翻译成认证权威或者认证中心,主要用途是为用户发放数字证书 功能:证书发放.证书更新.证书撤销和证书验证. 作用:身份认证, ...
- linux C access
[1]作用 确定文件的访问权限 [2]头文件 #include <unistd.h> [3]函数定义 int access(const char * pathname, int mode ...
- TPS54331 TPS54332 3.5V to 28V Input, 3A, 570kHz Step-Down Converter with Eco-mode
The TPS54331 is a 28-V, 3-A non-synchronous buck converter that integrates a low RDS(on) high side M ...
- SSH服务理论+实践
1)远程管理服务知识介绍 SSH远程登录服务介绍说明 01. SSH-Secure Shell Protocol 安全加密shel协议 SSH远程登录服务功能作用 01. 提供类似telnet远程登录 ...