Leetcode 558. Logical OR of Two Binary Grids Represented as Quad
A Binary Grid is a grid where each cell is either 0 or 1. A Quad-Tree is a tree data structure with at most four children. Each node in this tree represents a rectangular sub-grid of a binary grid. The root node represents the entire binary grid, and for any node representing some sub-grid, its four children represent the four quadrants of that sub-grid.
Given two binary grids represented by two Quad-Trees, return a Quad-Tree which represents the logical OR (or union) of the two grids.
Each Quad-Tree node has the following attributes:
bool val
: True if the node represents a grid of 1’s, and False if it represents a grid of 0’s.bool isLeaf
: True if the node is a leaf node, and False if the node has four children nodes in the tree.Node* topLeft
, Node* topRight
, Node* bottomLeft
, Node* bottomRight
: These are the four children of the current node.Note:
True
(1), the result will be a leaf with True
since the OR operation with any value and True
yields True
.False
(0), the result will depend entirely on the other node.Here’s the C++ implementation:
#include <iostream>
// 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) {
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:
Node* intersect(Node* quadTree1, Node* quadTree2) {
if (quadTree1->isLeaf) {
return quadTree1->val ? new Node(true, true) : quadTree2;
}
if (quadTree2->isLeaf) {
return quadTree2->val ? new Node(true, true) : quadTree1;
}
// Recursive call for each quadrant
Node* topLeft = intersect(quadTree1->topLeft, quadTree2->topLeft);
Node* topRight = intersect(quadTree1->topRight, quadTree2->topRight);
Node* bottomLeft = intersect(quadTree1->bottomLeft, quadTree2->bottomLeft);
Node* bottomRight = intersect(quadTree1->bottomRight, quadTree2->bottomRight);
// If all four children are leaves and have the same value, merge them into one leaf node
if (topLeft->isLeaf && topRight->isLeaf && bottomLeft->isLeaf && bottomRight->isLeaf &&
topLeft->val == topRight->val &&
topRight->val == bottomLeft->val &&
bottomLeft->val == bottomRight->val) {
return new Node(topLeft->val, true);
}
return new Node(false, false, topLeft, topRight, bottomLeft, bottomRight);
}
};
n
is the total number of nodes in the Quad-Tree. This is because the algorithm must potentially visit each node in both trees, resulting in a linear scan.By following this approach, we ensure that the logical OR of two Quad-Trees is computed efficiently and accurately.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?