100.Same Tree(E)
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)的更多相关文章
- 100. Same Tree(C++)
100. Same Tree Given two binary trees, write a function to check if they are equal or not. Two binar ...
- 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; ...
- <LeetCode OJ> 100. Same Tree
100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...
- 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 ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
- 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 ...
- 100. Same Tree
[题目] Given two binary trees, write a function to check if they are equal or not. Two binary trees ar ...
- leetcode 100. Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
随机推荐
- Python——Flask框架——数据库
一.数据库框架 Flask-SQLAlchemy (1)安装: pip install flask-sqlalchemy (2)Flask-SQLAlchemy数据库URL 数据库引擎 URL MyS ...
- js写插件教程入门
原文地址:https://github.com/lianxiaozhuang/blog 转载请注明出处 1. 点击add可以添加个自input的内容到div里并实现变颜色 <div id=& ...
- Java 下载 HLS (m3u8) 视频
下载索引文件 public String getIndexFile() throws Exception{ URL url = new URL(originUrlpath); //下载资源 Buffe ...
- codeforces618B
Guess the Permutation CodeForces - 618B Bob has a permutation of integers from 1 to n. Denote this p ...
- [BZOJ 2186] [SDOI 2008] 沙拉公主的困惑
Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为 \(1\) 到 \(N\) 的阶乘,但是,政府只发行编号与 \(M!\) 互质的钞票.房地产第 ...
- pysphere VMware控制模块的一些函数的说明
对于虚拟机的操作获得虚拟机对象 当你正常连接了服务器后,你就可以使用以下两种方式来得到虚拟机对象. get_vm_by_path get_vm_by_name 虚拟机路径可以从虚拟机右键信息中的”Ed ...
- BZOJ4128Matrix——hash+矩阵乘法+BSGS
题目描述 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) 输入 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * n的矩阵A.接下来一个n * n的矩阵B 输出 输出 ...
- Joseph POJ - 1012 约瑟夫环递推
题意:约瑟夫环 初始前k个人后k个人 问m等于多少的时候 后k个先出去 题解:因为前k个位置是不动的,所以只要考虑每次递推后的位置在不在前面k个就行 有递推式 ans[i]=(ans[i-1]+m ...
- 用贝叶斯定理解决三门问题并用Python进行模拟(Bayes' Rule Monty Hall Problem Simulation Python)
三门问题(Monty Hall problem)也称为蒙提霍尔问题或蒙提霍尔悖论,出自美国的电视游戏节目<Let’s Make a Deal>.问题名字来自该节目的主持人蒙提·霍尔(Mon ...
- Tournament ZOJ - 4063 (青岛区域赛 F 打表)
打表题.. 规律是找出来了 奈何优化不了 .... #include <iostream> #include <cstdio> #include <sstream> ...