当前位置:网站首页>小黑leetcode之旅:100 相同的树
小黑leetcode之旅:100 相同的树
2022-07-22 19:35:00 【小黑无敌】
小黑解法:树相等=结构相等+先/中根遍历的val值相等
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
pre = []
mid = []
# i判断是否树的结构相等,使用先根进行标注索引i
self.i = 0
# 先根遍历
def pre_node(node):
if node:
pre.append(node.val)
# 重新赋予val值,避免val重复带来的判断失误
node.val = self.i
self.i += 1
pre_node(node.left)
pre_node(node.right)
def mid_node(node):
if node:
mid_node(node.left)
mid.append(node.val)
mid_node(node.right)
pre_node(p)
mid_node(p)
p_pre = pre[:]
p_mid = mid[:]
pre = []
mid = []
self.i= 0
pre_node(q)
mid_node(q)
if p_pre == pre and p_mid == mid:
return True
return False

深度优先搜索
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
# p和q都为空的情况下
if not p and not q:
return True
# 只有一个为空
if not p or not q:
return False
# 全不为空的情况下
if p.val != q.val:
return False
return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)

宽度优先搜索
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
# 全为空
if not p and not q:
return True
# 有一个为空
if not p or not q:
return False
# 初始化队列
queue1 = collections.deque([p])
queue2 = collections.deque([q])
# 开始对队列进行操作
while queue1 and queue2:
# 队尾出栈
node1 = queue1.popleft()
node2 = queue2.popleft()
if node1.val != node2.val:
return False
# 左孩子进队
if node1.left and node2.left:
queue1.append(node1.left)
queue2.append(node2.left)
if (not node1.left and node2.left) or (node1.left and not node2.left):
return False
if node1.right and node2.right:
queue1.append(node1.right)
queue2.append(node2.right)
if (not node1.right and node2.right) or (node1.right and not node2.right):
return False
return not(queue1 or queue2)

边栏推荐
- 《STL容器篇》-string模拟实现
- 电脑屏幕变大了怎么还原 电脑屏幕显示缩放教程
- 电脑管理员权限怎么打开 管理员权限设置教程
- TCP四次挥手
- Flink data source disassembly and analysis (Wikipedia editssource)
- BGP Confederacy experiment
- Pikachu shooting range SQL injection search injection clearance steps
- 删除文件时需要system权限怎么办 你需要来自system的权限才能删除的解决办法
- Demo19- (to be updated)
- 常用正则表达式最强整理速查手册(荣耀典藏版)
猜你喜欢

Electromagnetic field and electromagnetic wave experiment 4. Be familiar with the application of CST Studio Software in the electromagnetic field

STL adapter stack and queue

Realize the national standard gb28181 streaming media service solution

Pikachu靶场-SQL注入-搜索型注入过关步骤

时代潮头,华为将风帆对准数字金融的风与海

事件抽取文献整理(2020-2021)

引擎提示Alias HeroDB跟游戏引擎启动异常怎么解决?

一篇搞定CAS,深度讲解,面试实践必备

Jupyternotebook runs to the specified line

OpenCV-一维频域滤波器
随机推荐
PHP 防止或检测页面被刷新 post重复提交问题
实现国标GB28181流媒体服务解决方案
时代潮头,华为将风帆对准数字金融的风与海
Stack et file d'attente de l'adaptateur STL
《STL容器篇》-string模拟实现
Realize OPC UA publish/subscribe single send
Pikachu shooting range SQL injection search injection clearance steps
Uric acid detection and precautions
《STL适配器》stack和queue
Combing the docking process between the integration base and the business system
thinkphp URL_MODE =0 普通模式 的具体用法
电脑不能截屏怎么办?电脑的快捷截屏键无法使用的解决办法
安防摄像头互联网直播方案LiveGBS设计文档
abap ALV总结整理
LUR缓存算法
电脑如何快速关机 电脑关机命令分享
如何将监控画面嵌入微信公众号进行直播
HCDE城市闭门会南京站成功举办
Kali-工具-sqlmap常见用法
电脑一拖二显示器分辨率怎么调? 两个显示器设置不同分辨率的技巧