-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
74 lines (67 loc) · 2.19 KB
/
Solution.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
/*
// 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) {
return build(grid, 0, grid.size(), 0, grid.size()) ;
}
Node *build(vector<vector<int>> &g, int l, int r, int t, int b)
{
Node *node = new Node ;
node->topLeft = NULL ;
node->topRight = NULL ;
node->bottomLeft = NULL ;
node->bottomRight = NULL ;
node->isLeaf = false ;
bool tl, tr, bl, br ;
if (l + 1 == r)
{
node->val = g[t][l] ;
node->isLeaf = true ;
return node ;
}
int vmid = (l+r)>>1 ;
int hmid = (t+b)>>1 ;
node->topLeft = build(g, l, vmid, t, hmid) ;
node->topRight = build(g, vmid, r, t, hmid) ;
node->bottomLeft = build(g, l, vmid, hmid, b) ;
node->bottomRight = build(g, vmid, r, hmid, b) ;
if (node->topLeft->isLeaf && node->topRight->isLeaf && node->bottomLeft->isLeaf && node->bottomRight->isLeaf)
{
if (node->topLeft->val && node->topRight->val && node->bottomLeft->val && node->bottomRight->val
|| !(node->topLeft->val || node->topRight->val || node->bottomLeft->val || node->bottomRight->val))
{
node->val = node->topLeft->val ;
node->isLeaf = true ;
delete(node->topLeft) ;
delete(node->topRight) ;
delete(node->bottomLeft) ;
delete(node->bottomRight) ;
node->topLeft = NULL ;
node->topRight = NULL ;
node->bottomLeft = NULL ;
node->bottomRight = NULL ;
}
}
return node ;
}
};