-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path558.cpp
51 lines (47 loc) · 1.51 KB
/
558.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
/*
// 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* intersect(Node* quadTree1, Node* quadTree2) {
if(quadTree1->isLeaf){
if(quadTree1->val)
return quadTree1;
else
return quadTree2;
}
if(quadTree2->isLeaf){
if(quadTree2->val)
return quadTree2;
else
return quadTree1;
}
// case four node has the same value and is the leaf, merge them
Node* tl = intersect(quadTree1->topLeft, quadTree2->topLeft);
Node* tr = intersect(quadTree1->topRight, quadTree2->topRight);
Node* bl = intersect(quadTree1->bottomLeft, quadTree2->bottomLeft);
Node* br = intersect(quadTree1->bottomRight, quadTree2->bottomRight);
if(tl->val == tr->val && tl->val == bl->val && tl->val == br->val && tl->isLeaf && tr->isLeaf && bl->isLeaf && br->isLeaf)
return new Node(tl->val, true, NULL, NULL, NULL, NULL);
else
return new Node(false, false, tl, tr, bl, br);
}
};