[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 ...
随机推荐
- 160A
#include <stdio.h> int main() { int n; int sum1=0, sum2=0, sum3=0; int x, y, z; scanf("%d ...
- windows程序设计 基础
API全名(Application Program Interface) Windows窗口主函数 int WINAPI WinMain( HINSTANCE hInstance,//应用程序本次运行 ...
- Hbase 读写 原理
客户端读取信息流程 ()client要读取信息,先查询下client 端的cache中是否存在数据,如果存在,刚直接返回数据.如果不存在,则进入到zookeeper,查找到里面的相应数据存在的Root ...
- 配置 ROS 的 apt 源
配置 ROS 的 apt 源 ROS的apt源有官方源.国内 USTC 源或新加坡源可供选择, 选择其一就可以了,建议使用国内 USTC 源或新加坡源,安装速度会快很多. 方式一:官方源 $ sudo ...
- PowerShell 语法备忘
1.挡可能出现 .0 的时候需要加上引号 2.使用 -Join 或者 + 进行字符串拼接 3.在cmd 命令下可以使用 set /a s=1+2 表示需要进行计算,而不是拼接
- angular+webpack(二)
上篇文章Angular2开发基础之TSC编译 解决如何使用TSC来编译ng2项目,以及如何解决出现的error.这些点是新手容易忽视的内容, 要熟悉ng开发的工具链,还是需要掌握其中的重点.本篇文章是 ...
- [antd-design-pro] mock 数据(post,request不一致)Sorry, we need js to run correctly!
Sorry, we need js to run correctly! 可能问题: mock数据 api 和 request api 不一致 'POST /api/banners/left' ...
- usdt节点启动慢和队列深度超出了范围问题
usdt节点启动慢和队列深度超出了范围问题 usdt的连接节点报错Work queue depth exceeded(队列深度超出了范围)大概是什么问题?重启了几次节点都不行队列深度超出了范围,估计是 ...
- LeetCode258 各位相加
题目链接:https://leetcode-cn.com/problems/add-digits/ 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: ...
- 校验input 修改当前值的校验获取值方式
<input style="height: 18px; width: 90px;" name="nowQty" value="${ad.nowQ ...