Problem
We want to use quad trees to store an N x N
boolean grid. Each cell in the grid can only be true or false. The root node represents the whole grid. For each node, it will be subdivided into four children nodes until the values in the region it represents are all the same.
Each node has another two boolean attributes : isLeaf
and val
. isLeaf
is true if and only if the node is a leaf node. The val
attribute for a leaf node contains the value of the region it represents.
Your task is to use a quad tree to represent a given grid. The following example may help you understand the problem better:
Given the 8 x 8
grid below, we want to construct the corresponding quad tree:
It can be divided according to the definition above:
The corresponding quad tree should be as following, where each node is represented as a (isLeaf, val)
pair.
For the non-leaf nodes, val
can be arbitrary, so it is represented as *
.
Note:
N
is less than1000
and guaranteened to be a power of 2.- If you want to know more about the quad tree, you can refer to its wiki.
Solution: Recursion
Time complexity: O(n^2*logn)
Space complexity: O(n^2)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// Author: Huahua // Running time: 44 ms class Solution { public: Node* construct(vector<vector<int>>& grid) { return construct(grid, 0, 0, grid.size()); } private: Node* construct(const vector<vector<int>>& grid, int x, int y, int n) { if (n == 0) return nullptr; bool all_zeros = true; bool all_ones = true; for (int i = y; i < y + n; ++i) for (int j = x; j < x + n; ++j) if (grid[i][j] == 0) all_ones = false; else all_zeros = false; if (all_zeros || all_ones) return new Node(all_ones, true, nullptr, nullptr, nullptr, nullptr); return new Node(true, false, construct(grid, x, y, n/2), // topLeft construct(grid, x + n/2, y, n/2), // topRight construct(grid, x, y + n/2, n/2), // bottomLeft construct(grid, x + n/2, y + n/2, n/2)); // bottomRight } }; |
V2
Time complexity: O(n^2)
Space complexity: O(n^2)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// Author: Huahua // Running time: 56 ms class Solution { public: Node* construct(vector<vector<int>>& grid) { return construct(grid, 0, 0, grid.size()); } private: Node* construct(const vector<vector<int>>& grid, int x, int y, int n) { if (n == 0) return nullptr; if (n == 1) return new Node(grid[y][x], true, nullptr, nullptr, nullptr, nullptr); auto tl = construct(grid, x, y, n/2); // topLeft auto tr = construct(grid, x + n/2, y, n/2); // topRight auto bl = construct(grid, x, y + n/2, n/2); // bottomLeft auto br = construct(grid, x + n/2, y + n/2, n/2); // bottomRight if (tl->isLeaf && tr->isLeaf && bl->isLeaf && br->isLeaf && tl->val == tr->val && tl->val == bl->val && tl->val == br->val) { auto root = new Node(tl->val, true, nullptr, nullptr, nullptr, nullptr); delete tl; delete tr; delete bl; delete br; return root; } return new Node(true, false, tl, tr, bl, br); } }; |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment