四叉树是一种树数据,其中每个结点恰好有四个子结点:topLeft、topRight、bottomLeft 和 bottomRight。四叉树通常被用来划分一个二维空间,递归地将其细分为四个象限或区域。
我们希望在四叉树中存储 True/False 信息。四叉树用来表示 N * N 的布尔网格。对于每个结点, 它将被等分成四个孩子结点直到这个区域内的值都是相同的。每个节点都有另外两个布尔属性:isLeaf 和 isLeaf。当这个节点是一个叶子结点时 isLeaf 为真。val 变量储存叶子结点所代表的区域的值。
例如,下面是两个四叉树 A 和 B:
A:
+-------+-------+ T: true
| | | F: false
| T | T |
| | |
+-------+-------+
| | |
| F | F |
| | |
+-------+-------+
topLeft: T
topRight: T
bottomLeft: F
bottomRight: F
B:
+-------+---+---+
| | F | F |
| T +---+---+
| | T | T |
+-------+---+---+
| | |
| T | F |
| | |
+-------+-------+
topLeft: T
topRight:
topLeft: F
topRight: F
bottomLeft: T
bottomRight: 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 的整次幂。- 如果你想了解更多关于四叉树的知识,你可以参考这个 wiki 页面。
- 逻辑或的定义如下:如果
A 为 True,或者B 为 True,或者A 和 B 都为 True,则 "A 或 B" 为 True。
在真实的面试中遇到过这道题?
解题思路:
递归。主要在于理清其中的逻辑关系。
- 当合并的两个结点都是叶子节点时,最容易,逻辑取或即可。
- 只有其中一个结点是叶子节点时,如果叶子节点val=false,返回另一个结点。若这个叶子节点val=true,返回叶子节点。
- 如果两个结点都不是叶子,那么,递归的合并它们的4个孩子,一定要对应。如果合并以后,4个孩子都是叶子,并且它们的val相等,那么4个孩子合并,逻辑值val与孩子相同,当前的结点设置为叶子。

| static auto x = []() { std::ios::sync_with_stdio(false); std::cin.tie(0); return 0; }(); class Solution { public: Node* intersect(Node* quadTree1, Node* quadTree2) { Node* root; if (quadTree1->isLeaf == true) { if (quadTree1->val == true) root = quadTree1; else root = quadTree2; return root; } if (quadTree2->isLeaf == true) { if (quadTree2->val == true) root = quadTree2; else root = quadTree1; return root; } root = quadTree1; root->bottomLeft = intersect(quadTree1->bottomLeft, quadTree2->bottomLeft); root->bottomRight = intersect(quadTree1->bottomRight, quadTree2->bottomRight); root->topLeft = intersect(quadTree1->topLeft, quadTree2->topLeft); root->topRight = intersect(quadTree1->topRight, quadTree2->topRight); root->val=false; if ((root->bottomLeft->isLeaf&&root->bottomRight->isLeaf&&root->topLeft->isLeaf&&root->topRight->isLeaf) &&((root->bottomLeft->val && root->bottomRight->val && root->topLeft->val && root->topRight->val) || (!root->bottomLeft->val && !root->bottomRight->val && !root->topLeft->val && !root->topRight->val))) { root->val = root->bottomLeft->val; root->bottomLeft = nullptr; root->bottomRight = nullptr; root->topLeft = nullptr; root->topRight = nullptr; root->isLeaf = true; } return root; } }; |

本文深入探讨了四叉树数据结构,详细讲解了如何通过递归方式实现两个四叉树的逻辑或运算,用于合并两个N*N的布尔网格。文章提供了清晰的逻辑流程和C++代码示例,帮助读者理解四叉树的合并过程。
772

被折叠的 条评论
为什么被折叠?



