当前位置:网站首页>leetcode:558. 四叉树交集【四叉树dfs】
leetcode:558. 四叉树交集【四叉树dfs】
2022-07-15 12:35:00 【白速龙王的回眸】

分析
它的意思是让两个四叉树每个小方格对应的值取交让后返回的四叉树
显然返回情况就是当一方是叶子的时候
如果一方是叶子,它的方格里面的值都一样,如果都是1,就返回一个全1的叶子;如果都是0,那么就返回不是叶子的另外一个四叉树
然后第二种情况就是dfs,因为都不是叶子,所以可以把四个角的交集都求出来,然后如果他们都是叶子且值都相等就可以合并成一个大的叶子,否则,就正常的生成四叉
ac code
""" # Definition for a QuadTree node. class Node: def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight): self.val = val self.isLeaf = isLeaf self.topLeft = topLeft self.topRight = topRight self.bottomLeft = bottomLeft self.bottomRight = bottomRight """
class Solution:
def intersect(self, quadTree1: 'Node', quadTree2: 'Node') -> 'Node':
# 有一个叶子
if quadTree1.isLeaf:
return Node(quadTree1.val, True) if quadTree1.val else quadTree2
if quadTree2.isLeaf:
# 优雅
return self.intersect(quadTree2, quadTree1)
# 都不是叶子
o1 = self.intersect(quadTree1.topLeft, quadTree2.topLeft)
o2 = self.intersect(quadTree1.topRight, quadTree2.topRight)
o3 = self.intersect(quadTree1.bottomLeft, quadTree2.bottomLeft)
o4 = self.intersect(quadTree1.bottomRight, quadTree2.bottomRight)
# 如果四个位置都可以变成相同的叶子
if o1.isLeaf and o2.isLeaf and o3.isLeaf and o4.isLeaf and o1.val == o2.val == o3.val == o4.val:
return Node(o1.val, True)
return Node(False, False, o1, o2, o3, o4)
总结
来自四叉树的dfs
边栏推荐
- Hcip review (2)
- Hcip notes (1)
- Can't help but want to bargain in the financial market? I advise you to make a decision after reading this article
- Two years ago, how were the leading players and blue chips in defi?
- 分布式数据库技术前瞻
- 【云原生】基于Kubevela华为云的Terraform addon开发
- vite 中配置@表示访问src下的文件
- 万字详解SSH(SSH登录原理+SSH配置+模拟实现SSH免密登录)
- [Xingguang 04] 2022 deep learning GPU form
- torch. Max () and numpy Discrimination of max () use
猜你喜欢

曾经,我对着AI客服喷了两分钟,它只回复了我的第一句话

lnmp架构php安装

世界首款抗量子攻击商用密码芯片 | 沐创

Award winning research | openeuler developer experience research questionnaire

vite 中配置@表示访问src下的文件
![[MySQL] multi table query](/img/9a/21b8996470203d7dfbb162ae0d9276.png)
[MySQL] multi table query

Number formatting

Comparison of xssfworkbook, sxssfworkbook and easyexcel reading Excel files

Go zero micro service practical series (v. how to write cache code)
![[visdom drawing] summary of visdom drawing in deep learning](/img/1d/534eb0d1c0f7108d8cb959bd66c65d.png)
[visdom drawing] summary of visdom drawing in deep learning
随机推荐
webview加载url提示net::ERROR_UNKNOWN_URL_SCHEME
仓储系统亮灯分拣
二叉树上的相等子树
解析协同办公“协同”为何意,数字化办公又如何轻松“破题”?
红外遥控氛围灯触摸芯片-DLT8SA15A-杰力科创
文件解析_Excel文件解析
Solve the comprehensive monitoring scheme for the dynamic ring state of small and medium-sized machine rooms
WebView loading URL prompt net:: error_ UNKNOWN_ URL_ SCHEME
Issue 99: flutter learning (2)
Issue 100: encapsulate the flitter component of short answer
学习总结笔记6(阁瑞钛伦特软件-九耶实训)
想找个大券商开户?现在通过手机股票开户是安全的吗?
Hcip notes (2)
想成为精英级开发者?请逼自己养成这10个习惯
【云原生】基于Kubevela华为云的Terraform addon开发
【开发教程17】AI语音人脸识别(会议记录仪/人脸打卡机)-AI人脸注册认证与识别
Talk about seven ways to realize asynchronous programming
RS485 connection | MODBUS intelligent LED three color audible and visual alarm machine room warning light with buzzer
MySQL multi table query, detailed to this extent, haven't you learned yet?, Including display inner connection, implicit inner connection, left connection, right connection and sub query in inner and
Sony's metauniverse layout