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. vscode git設置

    1.git官网https://git-scm.com/download/win 链接下载:64-bit Git for Windows Setup,不要下载Portable,体积太大了: 如果git官 ...

  2. 在 ubuntu 中安装 python3.5、 tornado、 pymysql

    一.在 ubuntu 中安装 python3.5 1.首先,在系统中是自带python2.7的.不要卸载,因为一些系统的东西是需要这个的.python2.7和python3.5是可以共存的. 命令如下 ...

  3. Qt evenFilter()与installEvenFilter()

    1, eventFilter 函数中实现事件过滤器.请注意:该函数在 QObject 类中声明为一个虚函数,因此只能由 QObject 的子类继承使用. 2, installEventFilter函数 ...

  4. BZOJ4514[Sdoi2016]数字配对——最大费用最大流

    题目描述 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对,并获得 ci ...

  5. BZOJ3277 串 【后缀数组】【二分答案】【主席树】

    题目分析: 用"$"连接后缀数组,然后做一个主席树求区间内不同的数的个数.二分一个前缀长度再在主席树上求不同的数的个数. 代码: #include<bits/stdc++.h ...

  6. Codeforces551 C. GukiZ hates Boxes

    二分答案 + 贪心 传送门:$>here<$ $Solution$ 二分时间+贪心验证.思维难度主要在验证上,然而坑人的点却在n的取值上.那么先来谈如何验证.在已知时间的条件下,能否用一种 ...

  7. 【BZOJ4944】【NOI2017】泳池 概率DP 常系数线性递推 特征多项式 多项式取模

    题目大意 有一个\(1001\times n\)的的网格,每个格子有\(q\)的概率是安全的,\(1-q\)的概率是危险的. 定义一个矩形是合法的当且仅当: 这个矩形中每个格子都是安全的 必须紧贴网格 ...

  8. hdu 2955 Robberies (01背包)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 思路:一开始看急了,以为概率是直接相加的,wa了无数发,这道题目给的是被抓的概率,我们应该先求出总的 ...

  9. 自学华为IoT物联网_06 智慧家庭物联网常见问题及解决方案

    点击返回自学华为IoT物流网 自学华为IoT物联网_06 智慧家庭物联网常见问题及解决方案 1. 家庭中遇到的问题 2.1 华为智慧家庭概念的发展历程 2.2 华为智慧家庭的解决方案架构 智慧家庭主要 ...

  10. 「POJ3696」The Luckiest number【数论,欧拉函数】

    # 题解 一道数论欧拉函数和欧拉定理的入门好题. 虽然我提交的时候POJ炸掉了,但是在hdu里面A掉了,应该是一样的吧. 首先我们需要求的这个数一定可以表示成\(\frac{(10^x-1)}{9}\ ...