Skip to content

Commit 0b4fb49

Browse files
committed
0558 solved.
1 parent 0e5154c commit 0b4fb49

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(cpp_0558)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_0558 main.cpp)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/// Source : https://leetcode.com/problems/logical-or-of-two-binary-grids-represented-as-quad-trees/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-07-14
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
10+
/// DFS
11+
/// Time Complexity: O(n)
12+
/// Space Complexity: O(h)
13+
14+
/// Definition for a QuadTree node.
15+
class Node {
16+
public:
17+
bool val;
18+
bool isLeaf;
19+
Node* topLeft;
20+
Node* topRight;
21+
Node* bottomLeft;
22+
Node* bottomRight;
23+
24+
Node() {
25+
val = false;
26+
isLeaf = false;
27+
topLeft = NULL;
28+
topRight = NULL;
29+
bottomLeft = NULL;
30+
bottomRight = NULL;
31+
}
32+
33+
Node(bool _val, bool _isLeaf) {
34+
val = _val;
35+
isLeaf = _isLeaf;
36+
topLeft = NULL;
37+
topRight = NULL;
38+
bottomLeft = NULL;
39+
bottomRight = NULL;
40+
}
41+
42+
Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {
43+
val = _val;
44+
isLeaf = _isLeaf;
45+
topLeft = _topLeft;
46+
topRight = _topRight;
47+
bottomLeft = _bottomLeft;
48+
bottomRight = _bottomRight;
49+
}
50+
};
51+
52+
53+
class Solution {
54+
public:
55+
Node* intersect(Node* quadTree1, Node* quadTree2) {
56+
return dfs(quadTree1, quadTree2);
57+
}
58+
59+
private:
60+
Node* dfs(Node* node1, Node* node2){
61+
62+
if(node1->isLeaf && node2->isLeaf){
63+
return new Node(node1->val | node2->val, true);
64+
}
65+
66+
Node* ret = new Node(0, false);
67+
ret->topLeft = dfs(node1->topLeft ? node1->topLeft : new Node(node1->val, true),
68+
node2->topLeft ? node2->topLeft : new Node(node2->val, true));
69+
ret->topRight = dfs(node1->topRight ? node1->topRight : new Node(node1->val, true),
70+
node2->topRight ? node2->topRight : new Node(node2->val, true));
71+
ret->bottomLeft = dfs(node1->bottomLeft ? node1->bottomLeft : new Node(node1->val, true),
72+
node2->bottomLeft ? node2->bottomLeft : new Node(node2->val, true));
73+
ret->bottomRight = dfs(node1->bottomRight ? node1->bottomRight : new Node(node1->val, true),
74+
node2->bottomRight ? node2->bottomRight : new Node(node2->val, true));
75+
76+
if(ret->topLeft->isLeaf && ret->topRight->isLeaf && ret->bottomLeft->isLeaf && ret->bottomRight->isLeaf &&
77+
same(ret->topLeft->val, ret->topRight->val, ret->bottomLeft->val, ret->bottomRight->val)){
78+
ret->val = ret->topLeft->val;
79+
ret->isLeaf = true;
80+
ret->topLeft = ret->topRight = ret->bottomLeft = ret->bottomRight = nullptr;
81+
}
82+
return ret;
83+
}
84+
85+
bool same(bool a, bool b, bool c, bool d){
86+
return a == b && a == c && a == d;
87+
}
88+
};
89+
90+
91+
int main() {
92+
93+
return 0;
94+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
585585
| | | | | | |
586586
| 556 | [Next Greater Element III](https://leetcode.com/problems/next-greater-element-iii/) | [solution](https://leetcode.com/problems/next-greater-element-iii/solution/)<br/>[缺:底层实现] | [C++](0501-1000/0556-Next-Greater-Element-III/cpp-0556/) | | |
587587
| 557 | [Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/description/) | [solution](https://leetcode.com/problems/reverse-words-in-a-string-iii/solution/) | [C++](0501-1000/0557-Reverse-Words-in-a-String-III/cpp-0557/) | | |
588-
| | | | | | |
588+
| 558 | [Logical OR of Two Binary Grids Represented as Quad-Trees](https://leetcode.com/problems/logical-or-of-two-binary-grids-represented-as-quad-trees/) | [] | [C++](0501-1000/0558-Logical-OR-of-Two-Binary-Grids-Represented-as-Quad-Trees/cpp-0558/) | | |
589589
| 559 | [Maximum Depth of N-ary Tree](https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/) | [solution](https://leetcode.com/problems/maximum-depth-of-n-ary-tree/solution/) | [C++](0501-1000/0559-Maximum-Depth-of-N-ary-Tree/cpp-0559/) | | |
590590
| 560 | [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | [solution](https://leetcode.com/problems/subarray-sum-equals-k/solution/) | [C++](0501-1000/0560-Subarray-Sum-Equals-K/cpp-0560/) | | |
591591
| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/description/) | [solution](https://leetcode.com/problems/array-partition-i/solution/) | [C++](0501-1000/0561-Array-Partition-I/cpp-0561/) | | |

0 commit comments

Comments
 (0)