Logical OR of Two Binary Grids Represented as Quad-Trees
A quad tree stores an n × n grid of 0s and 1s. A leaf stands for a square that is all one value (val). An inner node splits its square into four equal quarters, its children topLeft, topRight, bottomLeft and bottomRight. A square is split only when it holds both values.
You are given two quad trees, quadTree1 and quadTree2, for two grids of the same size. Return the quad tree of their cell-by-cell OR: a cell is 1 when either grid has a 1 there. The result must be a proper quad tree too, so any quarter that comes out all one value is a single leaf.
Trees are written level by level as [isLeaf, val] pairs, with null for the empty child slots under a leaf. The val of an inner node does not matter.
Example 1
Input: quadTree1 = tree of [[0,0,1,1],[0,0,1,1],[1,0,0,0],[0,1,0,0]],
quadTree2 = tree of [[0,0,0,1],[0,0,1,0],[0,1,1,1],[1,0,1,1]]
Output: [[0,1],[1,0],[1,1],[1,1],[1,1]]
Why: the OR grid is 0 in the top-left quarter and 1 everywhere else2 more examples and the constraints
Example 2
Input: quadTree1 = [[1,0]], quadTree2 = [[1,1]] Output: [[1,1]] Why: a 1 × 1 grid: 0 OR 1 is 1
Example 3
Input: quadTree1 = [[1,1]], quadTree2 = tree of [[1,0],[0,1]] Output: [[1,1]] Why: the first grid is all 1s, so the OR is all 1s
Constraints
- Both trees are valid quad trees for grids of the same size n × n
- n = 2ˣ with 0 ≤ x ≤ 9, so n ≤ 512
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Expand to grids, rebuildO(n²)
- Recurse on both treesO(m)
The idea in one sentence
Side by side
| Approach | Time | Relative work for a 512 × 512 grid | Verdict |
|---|---|---|---|
| Expand to grids, rebuild | O(n²) | Simple; ignores the tree | |
| Recurse on both trees | O(m) | Best; stops at the first leaf |
n = side of the grid, m = number of nodes in the two trees (m can be far smaller than n²).
Edge cases to test
- Two single leaves. [[1,0]] and [[1,1]] give [[1,1]]: the whole grid is one cell or one value.
- A leaf of 1s. If either root is a leaf of 1s, the answer is that leaf.
- A leaf of 0s. If one root is a leaf of 0s, the answer is the other tree unchanged.
- Quarters that become equal. Two split quarters can OR to all 1s; they must merge into one leaf, as in Example 1.
- Merge that ripples up. If all four quarters merge to 1s, the root merges too and the answer is [[1,1]].
Common mistakes
- Forgetting the merge step, so the result has four equal leaves under a node that should be a leaf.
- Returning the leaf of 0s instead of the other tree, which erases the other grid's 1s.
- Reading
valon inner nodes: it means nothing there and may be either value.
How to talk through it
- 1Clarify. Do the grids have the same size? Can I reuse input nodes in the answer? Must the result be merged?
- 2Give the grid approach. Expand both, OR the cells, rebuild: correct but O(n²) even for tiny trees.
- 3Use the leaves. A leaf of 1s decides its square; a leaf of 0s hands over the other side.
- 4Code the recursion. Two leaf checks, four recursive calls, then merge equal leaves.
- 5Test out loud. Run two single leaves, a leaf of 1s against a deep tree, and quarters that merge.