-
Notifications
You must be signed in to change notification settings - Fork 3
/
558.cpp
71 lines (67 loc) · 2.05 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
// 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:
// both are leave -> or
// only one is leaf
// -> true => just leaf one
// -> false => return not leaf one
// both are not leave => get each nodes
Node* dfs(Node* quadTree1, Node* quadTree2) {
if (quadTree1->isLeaf && quadTree2->isLeaf) {
bool res = quadTree1->val | quadTree2->val;
return new Node(res, true);
}
if (quadTree1->isLeaf) {
if (quadTree1->val == true) return quadTree1;
else return quadTree2;
}
if (quadTree2->isLeaf) {
if (quadTree2->val == true) return quadTree2;
else return quadTree1;
}
Node* tl = dfs(quadTree1->topLeft, quadTree2->topLeft);
Node* tr = dfs(quadTree1->topRight, quadTree2->topRight);
Node* bl = dfs(quadTree1->bottomLeft, quadTree2->bottomLeft);
Node* br = dfs(quadTree1->bottomRight, quadTree2->bottomRight);
if (tl->isLeaf && tr->isLeaf && bl->isLeaf && br->isLeaf && tl->val && tr->val && bl->val && br->val) return new Node(true, true);
return new Node(false, false, tl, tr, bl, br);
}
Node* intersect(Node* quadTree1, Node* quadTree2) {
return dfs(quadTree1, quadTree2);
}
};