|
| 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 | +} |
0 commit comments