leetcode 558. 四叉树交集(Quad Tree Intersection)

题目描述:

四叉树是一种树数据,其中每个结点恰好有四个子结点:topLefttopRightbottomLeftbottomRight。四叉树通常被用来划分一个二维空间,递归地将其细分为四个象限或区域。

我们希望在四叉树中存储 True/False 信息。四叉树用来表示 N * N 的布尔网格。对于每个结点, 它将被等分成四个孩子结点直到这个区域内的值都是相同的。每个节点都有另外两个布尔属性:isLeafval。当这个节点是一个叶子结点时 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   |
|       |       |  |       |       |  |       |       |
+-------+-------+  +-------+-------+  +-------+-------+

提示:

  1. A 和 B 都表示大小为 N * N 的网格。
  2. N 将确保是 2 的整次幂。
  3. 如果你想了解更多关于四叉树的知识,你可以参考这个 wiki 页面。
  4. 逻辑或的定义如下:如果 A 为 True ,或者 B 为 True ,或者 A 和 B 都为 True,则 "A 或 B" 为 True。

解法:

/*
// 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* quadTree1, Node* quadTree2) {
        if(!quadTree1){
            return quadTree2;
        }else if(!quadTree2){
            return quadTree1;
        }else{
            if(quadTree1->isLeaf){
                if(quadTree1->val == true){
                    return quadTree1;
                }else{
                    return quadTree2;
                }
            }else if(quadTree2->isLeaf){
                if(quadTree2->val == true){
                    return quadTree2;
                }else{
                    return quadTree1;
                }
            }else{
                Node* root = new Node(false, false, NULL, NULL, NULL, NULL);
                root->topLeft = intersect(quadTree1->topLeft, quadTree2->topLeft);
                root->topRight = intersect(quadTree1->topRight, quadTree2->topRight);
                root->bottomLeft = intersect(quadTree1->bottomLeft, quadTree2->bottomLeft);
                root->bottomRight = intersect(quadTree1->bottomRight, quadTree2->bottomRight);
                if(root->topLeft->isLeaf && root->topRight->isLeaf 
                   && root->bottomLeft->isLeaf && root->bottomRight->isLeaf){
                    if(root->topLeft->val && root->topRight->val 
                       && root->bottomLeft->val && root->bottomRight->val){
                        root->isLeaf = true;
                        root->val = true;
                        root->topLeft = NULL;
                        root->topRight = NULL;
                        root->bottomLeft = NULL;
                        root->bottomRight = NULL;
                    }else if(!root->topLeft->val && !root->topRight->val 
                       && !root->bottomLeft->val && !root->bottomRight->val){
                        root->isLeaf = true;
                        root->val = false;
                        root->topLeft = NULL;
                        root->topRight = NULL;
                        root->bottomLeft = NULL;
                        root->bottomRight = NULL;
                    }
                }
                return root;
            }
        }
    }
};

转载于:https://www.cnblogs.com/zhanzq/p/10598810.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值