Construct Quad Tree - Problem

Given an n × n matrix grid of 0's and 1's only, we want to represent the grid with a Quad-Tree.

Return the root of the Quad-Tree representing grid.

A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:

  • val: True if the node represents a grid of 1's or False if the node represents a grid of 0's. Notice that you can assign the val to True or False when isLeaf is False, and both are accepted in the answer.
  • isLeaf: True if the node is a leaf node on the tree or False if the node has four children.

We can construct a Quad-Tree from a two-dimensional area using the following steps:

  1. If the current grid has the same value (i.e all 1's or all 0's) set isLeaf True and set val to the value of the grid and set the four children to Null and stop.
  2. If the current grid has different values, set isLeaf to False and set val to any value and divide the current grid into four sub-grids. Recurse for each of the children with the proper sub-grid.

Input & Output

Example 1 — Basic 2×2 Grid
$ Input: grid = [[1,1],[0,0]]
Output: [[0,1],[1,1],[1,1],[1,0],[1,0]]
💡 Note: Top half is all 1's (uniform), bottom half is all 0's (uniform), so root has 4 children: top-left leaf(1), top-right leaf(1), bottom-left leaf(0), bottom-right leaf(0)
Example 2 — All Same Values
$ Input: grid = [[1,1],[1,1]]
Output: [[1,1]]
💡 Note: Entire grid is uniform (all 1's), so we get a single leaf node with val=1 and isLeaf=1
Example 3 — Complex 4×4 Grid
$ 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],null,null,null,null,[1,0],[1,1]]
💡 Note: Top-left 2×2 is all 1's (leaf), top-right 2×2 is all 0's (leaf), bottom half is all 1's but gets subdivided first then merged

Constraints

  • n == grid.length == grid[i].length
  • n is a power of 2
  • 1 ≤ n ≤ 64
  • grid[i][j] is either 0 or 1

Visualization

Tap to expand
Construct Quad Tree INPUT 2x2 Grid Matrix 1 1 0 0 TL TR BL BR Input Grid: [[1,1],[0,0]] Grid Properties: - Size: n x n (n=2) - Values: 0s and 1s only - Not uniform (mixed values) ALGORITHM STEPS 1 Check Uniformity Grid has mixed 0s and 1s 2 Divide into 4 Quadrants TL, TR, BL, BR sub-grids 3 Recurse Each Quadrant Each 1x1 is uniform (leaf) 4 Build Tree Structure Connect parent to children Quad Tree Structure Root 1 1 0 0 TL TR BL BR isLeaf: false (root), true (children) FINAL RESULT Quad Tree (Level Order) [isLeaf:0 val:1] [1,1] [1,1] [1,0] [1,0] Node Format: [isLeaf, val] isLeaf=1: leaf node val=1: all 1s, val=0: all 0s Output (Level Order): [[0,1],[1,1],[1,1], [1,0],[1,0],null,null,...] OK - Tree Constructed! 5 nodes total (1 root + 4 leaves) Key Insight: Divide and Conquer The Quad Tree recursively divides the grid into 4 equal quadrants until each region is uniform. Time: O(n^2 log n) - each cell visited O(log n) times. Space: O(log n) recursion depth. Base case: 1x1 grid or uniform grid becomes a leaf. Non-uniform grid splits into 4 children. TutorialsPoint - Construct Quad Tree | Divide and Conquer Approach
Asked in
Google 15 Facebook 12 Amazon 8
28.5K Views
Medium Frequency
~25 min Avg. Time
876 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