当前位置:网站首页>leetcode-每日一题558. 四叉树交集(分治递归)
leetcode-每日一题558. 四叉树交集(分治递归)
2022-07-31 05:10:00 【lin钟一】
题目链接:https://leetcode.cn/problems/logical-or-of-two-binary-grids-represented-as-quad-trees/
思路
方法一、分治递归
直接想法
四叉树相当于一个 n * n 的矩阵,四个节点又分成了父节点的矩阵的四个区域
- topLeft 节点为其父节点对应的矩阵区域左上角的 1/4 区域。
- topRight 节点为其父节点对应的矩阵区域右上角的 1/4 区域。
- bottomLeft 节点为其父节点对应的矩阵区域左下角的 1/4 区域。
- bottomRight 节点为其父节点对应的矩阵区域右下角的1/4 区域。
题目要求我们合并两颗四叉树 quadTree1 和 quadTree2,合并的方式就是把这矩阵中对应的四个区域的Val值进行 或 操作
或操作:x ∈ {0,1}, 0 | x = x, 1 | x = 1
所以我们需要把这两棵树对应的结合进行合并,设当前操作的两个结点为 node1 和 node2, 合并操作为 node1 | node2
- node1是叶结点
1)node1.Val 为1 ,那么 node1 | node2 = 1
2)node1.Val 为0,那么 node1 | node2 = node2 - node2是叶结点
1)node2.Val 为1 ,那么 node1 | node2 = 1
2)node2.Val 为0,那么 node1 | node2 = node1 - 两个结点都不是叶结点,则我们需要对两个结点的四个子节点分别进行分治递归操作,重复上面合并操作,然后再判断合并后的四个子结点是否对应的区域值都为全0 或者 全1,如果是则合并结点为叶子节点,反之则不是叶子节点,且四个子节点的值为上面合并操作后的四个对应子节点值
代码示例
/** * Definition for a QuadTree node. * type Node struct { * Val bool * IsLeaf bool * TopLeft *Node * TopRight *Node * BottomLeft *Node * BottomRight *Node * } */
func intersect(quadTree1, quadTree2 *Node) *Node {
//node1是叶子节点
if quadTree1.IsLeaf {
//node1值为1,则直接合并
if quadTree1.Val {
return &Node{
Val: true, IsLeaf: true}
}
//node1 | node2 = node2
return quadTree2
}
//node2是叶子节点
if quadTree2.IsLeaf {
//合并两个结点
return intersect(quadTree2, quadTree1)
}
//合并左上区域
TL := intersect(quadTree1.TopLeft, quadTree2.TopLeft)
//合并右上区域
TR := intersect(quadTree1.TopRight, quadTree2.TopRight)
//合并左下区域
BL := intersect(quadTree1.BottomLeft, quadTree2.BottomLeft)
//合并右下区域
BR := intersect(quadTree1.BottomRight, quadTree2.BottomRight)
//判断当前结点是否都是叶子结点并且是否为全0或全1
if TL.IsLeaf && TR.IsLeaf && BL.IsLeaf && BR.IsLeaf && TL.Val == TR.Val && TL.Val == BL.Val && TL.Val == BR.Val {
return &Node{
Val: TL.Val, IsLeaf: true}
}
return &Node{
false, false, TL, TR, BL, BR}
}
复杂度分析
- 时间复杂度:O(n2),其中 n 是四叉树 quadTree1 和 quadTree2 对应矩阵的边长。最坏的情况下,整个矩阵都会被遍历。
- 空间复杂度:O(logn),其中 n 是四叉树 quadTree1 和 quadTree2 对应矩阵的边长。空间开销主要为递归的空间开销。
边栏推荐
- 关于LocalDateTime的全局返回时间带“T“的时间格式处理
- 【LeetCode-SQL每日一练】——2. 第二高的薪水
- uni-app进阶之生命周期【day8】
- Distributed transaction processing solution big PK!
- 剑指offer基础版--- 第23天
- Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
- 剑指offer基础版 ----- 第25天
- 数据库上机实验2 单表查询和嵌套查询
- Refinement of the four major collection frameworks: Summary of List core knowledge
- 关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
猜你喜欢
C语言文件读、写、定位函数
数据库上机实验5 数据库安全性
Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
07 【内置指令 自定义指令】
13 【代理配置 插槽】
【MQ我可以讲一个小时】
精解四大集合框架:List 核心知识总结
第7章 网络层第3次练习题答案(第三版)
[MQ I can speak for an hour]
The interviewer asked me how to divide the database and the table?Fortunately, I summed up a set of eight-part essays
随机推荐
The interviewer asked me TCP three handshake and four wave, I really
闭包(五)----一个常见的循环
【MySQL8入门到精通】基础篇- Linux系统静默安装MySQL,跨版本升级
Mysql——字符串函数
12 【nextTick 过渡与动画】
Three handshakes and four waves
Linux系统安装mysql(rpm方式安装)
Lock wait timeout exceeded解决方案
tf.keras.utils.get_file()
联盟链的真实场景在哪里
Input length must be multiple of 8 when decrypting with padded cipher
C语言文件读、写、定位函数
详解扫雷游戏(C语言)
Why use Flink and how to get started with Flink?
Redis的初识
账号或密码多次输入错误,进行账号封禁
剑指offer基础版 ----- 第28天
uni-app进阶之内嵌应用【day14】
面试官问我TCP三次握手和四次挥手,我真的是
The process and specific code of sending SMS verification code using flask framework