427. 建立四叉树(Construct Quad Tree)

目录

    我们想要使用一棵四叉树来储存一个 N x N 的布尔值网络。网络中每一格的值只会是真或假。树的根结点代表整个网络。对于每个结点, 它将被分等成四个孩子结点直到这个区域内的值都是相同的。

    每个结点还有另外两个布尔变量: isLeafvalisLeaf 当这个节点是一个叶子结点时为真。val 变量储存叶子结点所代表的区域的值。

    你的任务是使用一个四叉树表示给定的网络。下面的例子将有助于你理解这个问题:

    给定下面这个8 x 8 网络,我们将这样建立一个对应的四叉树:

    由上文的定义,它能被这样分割:

    对应的四叉树应该像下面这样,每个结点由一对 (isLeaf, val) 所代表.

    对于非叶子结点,val 可以是任意的,所以使用*代替。

    提示:

    1. N 将小于 1000 且确保是 2 的整次幂。
    2. 如果你想了解更多关于四叉树的知识,你可以参考这个 wiki 页面。
    /*
    // 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* construct(vector<vector<int>>& grid) {
            int m = grid.size();
            // cout<<m<<endl;
            if(m == 0){
                return NULL;
            }else if(m == 1){
                if(grid[0][0] == 1){
                    return new Node(true, true, NULL, NULL, NULL, NULL);
                }else{
                    return new Node(false, true, NULL, NULL, NULL, NULL);
                }
            }else{
                vector<vector<int>> lt; // left top
                vector<vector<int>> rt; // right top
                vector<vector<int>> lb; // left bottom
                vector<vector<int>> rb; //right bottom
                int half = m/2;
                for(int i = 0; i < half; i++){
                    lt.push_back(vector<int>(grid[i].begin(), grid[i].begin() + half));
                    lb.push_back(vector<int>(grid[i+half].begin(), grid[i+half].begin() + half));
                    rt.push_back(vector<int>(grid[i].begin()+half, grid[i].end()));
                    rb.push_back(vector<int>(grid[i+half].begin()+half, grid[i+half].end()));
                }
                Node* _lt = construct(lt);
                Node* _rt = construct(rt);
                Node* _lb = construct(lb);
                Node* _rb = construct(rb);
                if(_lt->isLeaf && _rt->isLeaf && _lb->isLeaf && _rb->isLeaf){
                    if(_lt->val && _rt->val && _lb->val && _rb->val){
                        return new Node(true, true, NULL, NULL, NULL, NULL);
                    }else if(!_lt->val && !_rt->val && !_lb->val && !_rb->val){
                        return new Node(false, true, NULL, NULL, NULL, NULL);
                    }else{
                        return new Node(false, false, _lt, _rt, _lb, _rb);
                    }
                }else{
                    return new Node(false, false, _lt, _rt, _lb, _rb);
                }
            }
        }
    };
    
    posted @ 2019-03-22 17:24  zhanzq1  阅读(372)  评论(0)    收藏  举报