LeetCode 558. 四叉树交集(递归)
1. 题目
四叉树是一种树数据,其中每个结点恰好有四个子结点:topLeft、topRight、bottomLeft 和 bottomRight。四叉树通常被用来划分一个二维空间,递归地将其细分为四个象限或区域。
我们希望在四叉树中存储 True/False 信息。四叉树用来表示 N * N 的布尔网格。对于每个结点, 它将被等分成四个孩子结点直到这个区域内的值都是相同的。每个节点都有另外两个布尔属性:isLeaf 和 val。当这个节点是一个叶子结点时 isLeaf 为真。val 变量储存叶子结点所代表的区域的值。
例如,下面是两个四叉树 A 和 B:
A:
+-------+-------+ T: true
| | | F: false
| T | T |
| | |
+-------+-------+
| | |
| F | F |
| | |
+-------+-------+
topLeft: T
topRight: T
bottomLeft: F
bottomRight: FB:
+-------+---+---+
| | F | F |
| T +---+---+
| | T | T |
+-------+---+---+
| | |
| T | F |
| | |
+-------+-------+
topLeft: T
topRight:topLeft: FtopRight: FbottomLeft: TbottomRight: T
bottomLeft: T
bottomRight: F
你的任务是实现一个函数,该函数根据两个四叉树返回表示这两个四叉树的逻辑或(或并)的四叉树。
A: B: C (A or B):
+-------+-------+ +-------+---+---+ +-------+-------+
| | | | | F | F | | | |
| T | T | | T +---+---+ | T | T |
| | | | | T | T | | | |
+-------+-------+ +-------+---+---+ +-------+-------+
| | | | | | | | |
| F | F | | T | F | | T | F |
| | | | | | | | |
+-------+-------+ +-------+-------+ +-------+-------+
提示:
A 和 B 都表示大小为 N * N 的网格。
N 将确保是 2 的整次幂。
逻辑或的定义如下:如果 A 为 True ,或者 B 为 True ,或者 A 和 B 都为 True,则 “A 或 B” 为 True。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/quad-tree-intersection
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 解题
相关题目:LeetCode 427. 建立四叉树(递归)
/*
// Definition for a QuadTree node.
class Node {
public:bool val;bool isLeaf;Node* topLeft;Node* topRight;Node* bottomLeft;Node* bottomRight;Node() {}Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {val = _val;isLeaf = _isLeaf;topLeft = _topLeft;topRight = _topRight;bottomLeft = _bottomLeft;bottomRight = _bottomRight;}
};
*/
class Solution {public:Node* intersect(Node* q1, Node* q2) {if(q1->isLeaf || q2->isLeaf)//至少有一个是叶子(全0 or 全1){if(q1->isLeaf && q2->isLeaf)//都是叶子{bool v = q1->val || q2->val;//求并return new Node(v,true,0,0,0,0);}else if(q1->isLeaf){if(q1->val == false)//q1全为0,并集由q2决定return q2;elsereturn q1;//q1全为1,直接返回q1}else//q2->isLeaf{if(q2->val == false)//q2全为0,并集由q1决定return q1;elsereturn q2;//q2全为1,直接返回q2}}else//q1,q2都不是叶子,其下面有true,有false{Node *root = new Node(false,false,0,0,0,0);root->topLeft = intersect(q1->topLeft, q2->topLeft);root->topRight = intersect(q1->topRight, q2->topRight);root->bottomLeft = intersect(q1->bottomLeft, q2->bottomLeft);root->bottomRight = intersect(q1->bottomRight, q2->bottomRight);if(root->topLeft->isLeaf && root->topRight->isLeaf&& root->bottomLeft->isLeaf && root->bottomRight->isLeaf){ //四个子节点都是叶子bool topL = root->topLeft->val;if(root->topRight->val == topL && root->bottomLeft->val == topL&& root->bottomRight->val == topL){ //且四个子节点的值都相同,则root是叶子root->isLeaf = true;root->val = topL;//舍弃孩子(合并了)root->topLeft = root->topRight = root->bottomLeft= root->bottomRight = NULL;}}return root;}}
};
LeetCode 558. 四叉树交集(递归)相关推荐
- Java实现 LeetCode 558 四叉树交集(四叉树,第一次遇到,研究了半天)
558. 四叉树交集 四叉树是一种树数据,其中每个结点恰好有四个子结点:topLeft.topRight.bottomLeft 和 bottomRight.四叉树通常被用来划分一个二维空间,递归地将其 ...
- LeetCode 558. 四叉树交集
558. 四叉树交集 [分治+递归]这道题太长了,但是我们可以简单梳理一下: 1.如果两个节点任何一个为叶子: 如果叶子是1,那么直接返回为1的叶子就行 如果叶子是0,那么直接返回另一个节点 2.递归 ...
- leetcode 558. Logical OR of Two Binary Grids Represented as Quad-Trees | 558. 四叉树交集(分治法)
题目 https://leetcode.com/problems/logical-or-of-two-binary-grids-represented-as-quad-trees/ 题解 自己写的没通 ...
- LeetCode 757. 设置交集大小至少为2
LeetCode 757. 设置交集大小至少为2 题目描述 一个整数区间 [a, b] ( a < b ) 代表着从 a 到 b 的所有连续整数,包括 a 和 b. 给你一组整数区间interv ...
- LeetCode 427. 建立四叉树(递归)
1. 题目 我们想要使用一棵四叉树来储存一个 N x N 的布尔值网络.网络中每一格的值只会是真或假.树的根结点代表整个网络.对于每个结点, 它将被分等成四个孩子结点直到这个区域内的值都是相同的. 每 ...
- 427 建立四叉树(递归、二维前缀和)
1. 问题描述: 给你一个 n * n 矩阵 grid ,矩阵由若干 0 和 1 组成.请你用四叉树表示该矩阵 grid . 你需要返回能表示矩阵的四叉树的根结点. 注意,当 isLeaf 为 Fal ...
- leetcode题解(二叉树和递归问题)
这篇博客我们主要来看二叉树和二叉树问题中使用递归来解决问题的思路.这类问题有时思路很简单,这是因为二叉树具有天然的递归结构,所以我们使用递归的解法解决二叉树有关的问题就变得非常明显. 二叉树天然的递归 ...
- leetcode —— 46. 全排列(递归+回溯)
给定一个 没有重复 数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2 ...
- leetcode 101. 对称二叉树 递归解法 c语言
如题: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的.1/ \2 2/ \ / \ 3 4 4 3但是下面这个 [1,2,2,null,3,null ...
最新文章
- CSS 中功能相似伪类间的区别
- 空战决策知识构建方法研究
- linux 访问共享内存,Linux下的共享内存(03)---通过指针访问共享内存中的数据...
- 加密后变成乱码解密_个人磁盘加密软件,使用VeraCrypt进行整盘加密介绍
- 背起行囊,就是过客;放下包袱,就有归宿。
- 阿里“AI搭配师”一秒给你100种穿搭建议,程序员进军女性时尚靠什么?
- python 几何计算_计算几何-凸包算法 Python实现与Matlab动画演示
- java生成16位唯一性的订单号
- D3D 扎带 小样本
- 直流电机驱动电路整理笔记
- 《广义动量定理与系统思考——战争、…
- 在centos下安装pycrypto报错 RuntimeError: autoconf error
- 金蝶新建生产领料单,单据编号不能自动填写
- 电气潮流运算Matlab怎么编程,基于Matlab的电力系统潮流编程计算
- UGUI源码分析:GridLayoutGroup网格布局组件与ContentSizeFitter尺寸调节组件
- USB标准设备描述符
- 深耕DID,INTO钱包拿到进入Web3的钥匙
- linux kernel bridge 数据包流向
- 怎样判断一张银行卡是信用卡
- mvc viewresult html,return view 详解 MVC
热门文章
- html里嵌入语音,HTML5语音合成API语音/语言支持
- oracle 01013 02063,Oracle11g dblink用户密码大写限制-ORA-02063: preceding line from FOR244_DBLINK...
- 【作品】超级玛丽射击版
- CF Edu54 E. Vasya and a Tree DFS+树状数组
- C语言那年踩过的坑--局部变量,静态变量,全局变量在内存中存放的位置
- DetachedCriteria和Criteria的使用方法
- Sublime Text 3插件安装方法
- 待机、休眠、睡眠的区别和优缺点
- 前两年在MSDN里找到的HTC示例,一直没用过,先在这里存个备份
- 锡安赞歌 mp3下载