Quiz
#558 Logical OR of Two Binary Grids Represented as Quad-Trees
APPROACH
Two Quad-Trees, quadTree1 and quadTree2, each encode an n * n binary matrix. Return a Quad-Tree that represents the cell-wise logical OR of the two matrices. When isLeaf is False the val may be any value.
Each Quad-Tree node has:
- val: True when the region is all 1s; False when all 0s (relevant only for leaf nodes).
- isLeaf: True for uniform regions; False otherwise (node has four children).
Building a Quad-Tree: if the current region is uniform, make it a leaf with that value; otherwise split into four quadrants and recurse.
Serialization uses level-order traversal with null for absent nodes. Each node is encoded as [isLeaf, val] using 1 for True and 0 for False.
Example 1:
Example 2:
Example 3:
What is the optimal approach for this problem?