forked from zhuli19901106/leetcode-zhuli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstruct-quad-tree_1_AC.cpp
84 lines (77 loc) · 2.04 KB
/
construct-quad-tree_1_AC.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
// 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:
Solution() {
offsets = vector<vector<int>>{{0, 0}, {0, 1}, {1, 0}, {1, 1}};
}
Node* construct(vector<vector<int>>& grid) {
int n = grid.size();
if (n <= 0) {
return nullptr;
}
return constructRecursive(grid, 0, 0, n >> 1);
}
private:
vector<vector<int>> offsets;
bool isLeaf(vector<vector<int>>& grid, int x, int y, int i) {
if (i == 0) {
return true;
}
int x1, y1;
for (auto &offset: offsets) {
x1 = x + offset[0] * i;
y1 = y + offset[1] * i;
if (!isLeaf(grid, x1, y1, i >> 1)) {
return false;
}
if (grid[x][y] != grid[x1][y1]) {
return false;
}
}
return true;
}
Node* constructRecursive(vector<vector<int>>& grid, int x, int y, int i) {
Node* ptr;
bool leaf = isLeaf(grid, x, y, i);
if (i <= 0 || leaf) {
ptr = new Node(
(grid[x][y] != 0),
leaf,
nullptr,
nullptr,
nullptr,
nullptr
);
} else {
ptr = new Node(
(grid[x][y] != 0),
leaf,
constructRecursive(grid, x, y, i >> 1),
constructRecursive(grid, x, y + i, i >> 1),
constructRecursive(grid, x + i, y, i >> 1),
constructRecursive(grid, x + i, y + i, i >> 1)
);
}
return ptr;
}
};