100. same tree

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. Example 1: Input: 1 1
/ \ / \
2 3 2 3 [1,2,3], [1,2,3] Output: true Example 2: Input: 1 1
/ \
2 2 [1,2], [1,null,2] Output: false Example 3: Input: 1 1
/ \ / \
2 1 1 2 [1,2,1], [1,1,2] Output: false
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
from collections import deque class Solution:
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if (not p and q) or (p and not q):
return False
elif not p and not q:
return True
p_queue = deque([(1, p), ])
q_queue = deque([(1, q), ]) while p_queue and q_queue:
p_depth, p_root = p_queue.popleft()
q_depth, q_root = q_queue.popleft()
if p_depth != q_depth or p_root.val != q_root.val:
return False if p_root.left and q_root.left:
if p_root.left.val == q_root.left.val:
p_queue.append((p_depth + 1, p_root.left))
q_queue.append((q_depth + 1, q_root.left))
else:
return False
elif not p_root.left and not q_root.left:
pass
else:
return False if p_root.right and q_root.right:
if p_root.right.val == q_root.right.val:
p_queue.append((p_depth + 1, p_root.right))
q_queue.append((q_depth + 1, q_root.right))
else:
return False
elif not p_root.right and not q_root.right:
pass
else:
return False print("return..")
return True def isSameTree_32ms(self, p, q):
stack = [(p, q)]
while stack:
n1, n2 = stack.pop()
if n1 and n2 and n1.val == n2.val:
stack.append((n1.right, n2.right))
stack.append((n1.left, n2.left))
elif not n1 and not n2:
continue
else:
return False
return True def isSameTree_recursion36ms(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if (not p and q) or (not q and p): return False
if not (p and q): return True
if p.val == q.val:
return self.isSameTree_recursion36ms(p.left, q.left) and \
self.isSameTree_recursion36ms(p.right, q.right)
else:
return False

100.Same Tree(E)的更多相关文章

  1. 100. Same Tree(C++)

    100. Same Tree Given two binary trees, write a function to check if they are equal or not. Two binar ...

  2. LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number

    100. Same Tree /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; ...

  3. <LeetCode OJ> 100. Same Tree

    100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...

  4. LeetCode 100. Same Tree (判断树是否完全相同)

    100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...

  5. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

  6. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  7. LeetCode之100. Same Tree

    ------------------------------------------ 递归比较即可 AC代码: /** * Definition for a binary tree node. * p ...

  8. 100. Same Tree

    [题目] Given two binary trees, write a function to check if they are equal or not. Two binary trees ar ...

  9. leetcode 100. Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

随机推荐

  1. Lodop打印较大的超出纸张的图片

    ADD_PRINT_IMAGE打印图片时,如果一个图片过大,超出纸张,默认超出部分是不显示的,也不会分页.最近遇到有人利用ADD_PRINT_URL打印图片,说图片自动分了多页,因为这个方法一般是用来 ...

  2. 阿里云 ECS 安全组

    以前在案例云买的ECS我一般都是 连上 ssh,然后把网站文件拿上去 ,安装好需要的环境 然后就可以顺利的打开网站了,这次帮一个朋友买的阿里云ECS让我蒙了, 一切都准备好了 网站打不开 防火墙也检查 ...

  3. pixel和nexus设备安卓9.0/8.1/7.1.x/6.x WiFi和信号图标出现叉x号或者感叹号的消除办 法

    在安卓9.0/8.1/8.0/7.1.2里如何消除x号(在老一点点版本是感叹号)呢? 1.首先开启usb调试,然后用数据线连接电脑和手机. 2.然后解决好您的adb驱动问题,具体教程见:http:// ...

  4. 学习 Spring (九) 注解之 @Required, @Autowired, @Qualifier

    Spring入门篇 学习笔记 @Required @Required 注解适用于 bean 属性的 setter 方法 这个注解仅仅表示,受影响的 bean 属性必须在配置时被填充,通过在 bean ...

  5. 【python练习题】程序14

    #题目:将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5. #我的方法应该比网上的更加简洁,只是递归可能速度慢 n = input('请输入一个正整数:') n = int(n) X ...

  6. 通过JPA注解获取某个类的主键字段

    public String getPkColumn(String className) { String pkColumn = null; try { Class clazz = Class.forN ...

  7. Codeforces Round #545 Div. 1自闭记

    A:求出该行该列各有多少个比其小的取max,该行该列各有多少个比其大的取max,加起来即可. #include<iostream> #include<cstdio> #incl ...

  8. win10安装MySql 5.7.23

    下载安装 因为Django2.1不再支持MySQL5.5,这里需要重新安装一下MySQL 首先去官网下载 这里使用的是msi版本 https://dev.mysql.com/downloads/win ...

  9. 「AC自动机」学习笔记

    AC自动机(Aho-Corasick Automaton),虽然不能够帮你自动AC,但是真的还是非常神奇的一个数据结构.AC自动机用来处理多模式串匹配问题,可以看做是KMP(单模式串匹配问题)的升级版 ...

  10. 一种HBase表数据迁移方法的优化

    1.背景调研: 目前存在的hbase数据迁移主要分如下几类: 根据上图,可以看出: 其实主要分为两种方式:(1)hadoop层:因为hbase底层是基于hdfs存储的,所以可以通过把hdfs上的数据拷 ...