-
Notifications
You must be signed in to change notification settings - Fork 3
/
427.cpp
64 lines (60 loc) · 2 KB
/
427.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
/*
// Definition for a QuadTree node.
class Node {
public:
bool val;
bool isLeaf;
Node* topLeft;
Node* topRight;
Node* bottomLeft;
Node* bottomRight;
Node() {
val = false;
isLeaf = false;
topLeft = NULL;
topRight = NULL;
bottomLeft = NULL;
bottomRight = NULL;
}
Node(bool _val, bool _isLeaf) {
val = _val;
isLeaf = _isLeaf;
topLeft = NULL;
topRight = NULL;
bottomLeft = NULL;
bottomRight = NULL;
}
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* constructRecursieve(vector<vector<int>>& grid, int x1, int x2, int y1, int y2) {
if (x1 == x2) {
int value = (grid[x1][y1] == 1) ? true : false;
return new Node(value, true, nullptr, nullptr, nullptr, nullptr);
}
int midX = (x1 + x2) / 2;
int midY = (y1 + y2) / 2;
Node* topLeft = constructRecursieve(grid, x1, midX, y1, midY);
Node* topRight = constructRecursieve(grid, x1, midX, midY + 1, y2);
Node* bottomLeft = constructRecursieve(grid, midX + 1, x2, y1, midY);
Node* bottomRight = constructRecursieve(grid, midX + 1, x2, midY + 1, y2);
if (topLeft->isLeaf && topRight->isLeaf && bottomLeft->isLeaf && bottomRight->isLeaf &&
topLeft->val == topRight->val && topLeft->val == bottomLeft->val && topLeft->val == bottomRight->val) {
return new Node(topLeft->val, true, nullptr, nullptr, nullptr, nullptr);
}
return new Node(1, false, topLeft, topRight, bottomLeft, bottomRight);
}
Node* construct(vector<vector<int>>& grid) {
int n = grid.size();
return constructRecursieve(grid, 0, n - 1, 0, n - 1);
}
};