LeetCode--100--相同的树
问题描述:
给定两个二叉树,编写一个函数来检验它们是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
示例 1:
输入: 1 1
/ \ / \
2 3 2 3 [1,2,3], [1,2,3] 输出: true
示例 2:
输入: 1 1
/ \
2 2 [1,2], [1,null,2] 输出: false
示例 3:
输入: 1 1
/ \ / \
2 1 1 2 [1,2,1], [1,1,2] 输出: false
方法1:
class Solution:
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if not p or not q:
return p == q
left = self.isSameTree(p.left,q.left)
right = self.isSameTree(p.right,q.right)
return p.val == q.val and left and right
前序遍历:
class Solution:
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if p==None and q==None:
return True
if p==None:
return False
if q==None:
return False self.ans = True
self.preorder(p,q) return self.ans def preorder(self, p, q):
if p and q:
if p.val != q.val:
self.ans = False
return
self.preorder(p.left, q.left)
self.preorder(p.right, q.right)
elif q or q:
self.ans = False
return
2018-09-02 16:30:45
LeetCode--100--相同的树的更多相关文章
- LeetCode 100. 相同的树(Same Tree) 2
100. 相同的树 100. Same Tree 题目描述 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 每日一算法2019/5 ...
- Java实现 LeetCode 100 相同的树
100. 相同的树 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [ ...
- LeetCode 100.相同的树(C++)
给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1 ...
- LeetCode 100——相同的树
1. 题目 2. 解答 针对两棵树的根节点,有下列四种情况: p 和 q 都为空,两棵树相同: p 不为空 q 为空,两棵树不相同: p 为空 q 不为空,两棵树不相同: p 和 q 都不为空,如果两 ...
- LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40
572. 另一个树的子树 572. Subtree of Another Tree 题目描述 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 ...
- LeetCode 100 及 101题
100. 相同的树 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [ ...
- LeetCode刷题总结-树篇(下)
本文讲解有关树的习题中子树问题和新概念定义问题,也是有关树习题的最后一篇总结.前两篇请参考: LeetCode刷题总结-树篇(上) LeetCode刷题总结-树篇(中) 本文共收录9道题,7道中等题, ...
- LeetCode刷题总结-树篇(中)
本篇接着<LeetCode刷题总结-树篇(上)>,讲解有关树的类型相关考点的习题,本期共收录17道题,1道简单题,10道中等题,6道困难题. 在LeetCode题库中,考察到的不同种类的树 ...
- [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 (相同的树)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
随机推荐
- C++飞机大战
#include<windows.h> #include"resource.h" #include<stdlib.h> #include<time.h ...
- maven intall在target文件夹中自动生成的war包部署服务器时缺斤少两
1.问题描述,本地改动特别大或者升级系统操作,打war包部署服务器上程序时候,页面或者后台总是报错,原因就是比本地少东西. 2.问题排查解决:maven clean然后maven intall在tar ...
- eclipse里error报错Target runtime com.genuitec.runtime.generic.jee60 is not defined.
eclipse里error报错Target runtime com.genuitec.runtime.generic.jee60 is not defined. eclipse里error报错解决办法 ...
- tomcat 9.0.4 性能调优
参考了网上的一些优化参数,但是在启动中发现 有2个报错: 11-Feb-2018 15:57:23.293 警告 [main] org.apache.catalina.startup.SetAllPr ...
- Linux中Postfix邮件接收配置(四)
Dovecot介绍 MRA邮件取回代理也有很多如courier-imap,cyrus-imap和dovecot这三个个工具,下面重点介绍Dovecot: 1.高安全性.据 Dovecot 的作者声称, ...
- DNS正反向区域解析(二)
域名查询工具 Nslookup命令 >server 202.106.0.20 #指定DNS服务器 >set q=A #指定要查询的类型(A,PTR,MX,CNAME,NS) >www ...
- OpenGL边用边学------2 经典照相机模型
https://blog.csdn.net/smstong/article/details/50290327 实际照相步骤 1 布置场景和调整照相机位置 3 选择镜头对焦Focus 4 按下快门 5 ...
- Thinking in React 观后感
原文地址:Thinking in React 今天在翻阅 React 文档,看到一篇名为「Thinking in React」的文章觉得写的很好.文章介绍了如何使用 React 构建一个应用,并不是手 ...
- 网站精准查询IP
分享一些网站能精准查询IP的 https://www.chaidu.com/App/Web/IP/ http://www.ipip.net/ip.html http://www.hao7188.com ...
- 获取mips32机器的各数据类型的取值范围
一.背景: 使用的mips 32bit机器,32bit的vxworks操作系统(各机器带来的范围都不一样,与操作系统也有关联) 二.验证类型的范围: 2.1 unsigned long: void m ...