Construct Quad Tree - Problem

Construct Quad Tree is a fascinating problem that combines divide and conquer with tree data structures.

You're given an n Ɨ n matrix containing only 0s and 1s. Your task is to build a Quad-Tree representation of this matrix.

A Quad-Tree is a tree where each internal node has exactly four children representing four quadrants:

  • topLeft - upper-left quadrant
  • topRight - upper-right quadrant
  • bottomLeft - lower-left quadrant
  • bottomRight - lower-right quadrant

Each node has two attributes:

  • val - True if representing 1s, False if representing 0s
  • isLeaf - True if it's a leaf node, False if it has children

Construction Rules:

  1. If current region has uniform values (all 0s or all 1s): create a leaf node
  2. If current region has mixed values: create an internal node and recursively build four child subtrees

This data structure is widely used in image compression, spatial indexing, and computer graphics!

Input & Output

example_1.py — Simple 2Ɨ2 Matrix
$ Input: grid = [[0,1],[1,0]]
› Output: [[0,1],[1,0],[1,1],[1,1],[1,1]]
šŸ’” Note: The root node is not a leaf (isLeaf=0) so its value can be anything (val=1). It has four children: Top-left and bottom-right are leaf nodes with value 0 (represented as [1,0]). Top-right and bottom-left are leaf nodes with value 1 (represented as [1,1]).
example_2.py — Uniform Matrix
$ Input: grid = [[1,1],[1,1]]
› Output: [[1,1]]
šŸ’” Note: Since the entire matrix contains only 1s, we can represent it with a single leaf node with value 1. The output [1,1] means isLeaf=1 (true) and val=1 (true).
example_3.py — Larger Mixed Matrix
$ Input: grid = [[1,1,0,0],[1,1,0,0],[1,1,1,1],[1,1,1,1]]
› Output: [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]
šŸ’” Note: The 4Ɨ4 matrix gets divided into quadrants. Top-left is all 1s (leaf), top-right is all 0s (leaf), bottom-left is mixed (needs further division), bottom-right is all 1s (leaf). The mixed bottom-left quadrant gets divided into 4 more sub-quadrants.

Visualization

Tap to expand
01104Ɨ4 MatrixRoot0110Resulting Quad-TreeStep 1: Divide into quadrantsStep 2: Create leaf nodes for uniform regions
Understanding the Visualization
1
Start with Full Matrix
Begin with the entire nƗn matrix as the root region
2
Check Uniformity
Scan the region to see if all values are the same (all 0s or all 1s)
3
Create Leaf or Divide
If uniform, create leaf node. If mixed, divide into 4 equal quadrants
4
Recursive Processing
Apply the same process to each of the 4 quadrants
5
Build Tree Structure
Connect child nodes to parent nodes to form the complete quad-tree
Key Takeaway
šŸŽÆ Key Insight: Quad-trees efficiently represent spatial data by recursively subdividing regions until they become uniform, creating a hierarchical structure that saves space and enables fast spatial queries.

Time & Space Complexity

Time Complexity
ā±ļø
O(n² log n)

In worst case, we visit each cell once per level of recursion. With log n levels of recursion, total time is O(n² log n)

n
2n
⚠ Quadratic Growth
Space Complexity
O(log n)

Recursion stack depth is at most log n levels (when dividing nƗn matrix). Each recursive call uses constant space.

n
2n
⚔ Linearithmic Space

Constraints

  • n == grid.length == grid[i].length
  • n == 2x where 0 ≤ x ≤ 6
  • Matrix dimensions are powers of 2 (1, 2, 4, 8, 16, 32, 64)
  • grid[i][j] is either 0 or 1
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
48.7K Views
Medium Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen