Construct Quad Tree
LeetCode 427 · Medium · solution, intuition & step-by-step walkthrough
Uniform square → leaf; else recurse on 4 quadrants.
This is how I solved Construct Quad Tree (LeetCode 427), a medium problem from the Other pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.
Intuition
If every cell in a square has the same value, it's a single leaf. Otherwise split it into four equal quadrants and build each one recursively.
Approach
- 1
Check whether all cells in the current n×n square match.
- 2
Yes → leaf with that value.
- 3
No → halve n and build top-left, top-right, bottom-left, bottom-right.
- 4
Combine into an internal node.
How I solved it
Divide and conquerPythonO(n² log n) time
"""
# Definition for a QuadTree node.
class Node:
def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):
self.val = val
self.isLeaf = isLeaf
self.topLeft = topLeft
self.topRight = topRight
self.bottomLeft = bottomLeft
self.bottomRight = bottomRight
"""
"""
the problem looks like a recursive problem since we need to diviide the grid each time, n//2
point to note, if all cells of a quadrant are same, then it's a leaf node - can be base case, because for a quadrand size of 1 all nodes are same
else we need to buld a node
//how to check if all cells are same, will come to this later
n will be n//2
top left cell will start from row=0, col=0
top right cell will start from row=0, col=col+n
bottom left cell will start from row=row+n, col=0
bottom right cell will start from row=row+n, col=col+n
make a node and assign 4 quandrants
Now comes
//how to check if all cells are same
run two loops from i=0 to n and j = 0 to n
//starting position of a quadrant is row and col
so if first and adjacent cells are not same then all nodes are not same so it can't be a leaf node
if grid[row][col]!=grid[row+i][col+j] then all nodes are not same
"""
#SC - logn since each time we are dividing the grid by 2
#TC - O(n^2) since worst case we can compare all cells
class Solution:
def dfs(self,row,col,grid,n):
allCellsSame = True
for i in range(n):
for j in range(n):
if grid[row][col]!=grid[row+i][col+j]:
allCellsSame=False
if allCellsSame:
val = True if grid[row][col]==1 else 0
return Node(val,True)
n=n//2
topLeftCell = self.dfs(row,col,grid,n)
topRightCell = self.dfs(row,col+n,grid,n)
bottomLeftCell = self.dfs(row+n,col,grid,n)
bottomRightCell = self.dfs(row+n,col+n,grid,n)
return Node(0,False,topLeftCell,topRightCell,bottomLeftCell,bottomRightCell)
def construct(self, grid: List[List[int]]) -> 'Node':
n = len(grid)
return self.dfs(0,0,grid,n)
Construct Quad Tree — FAQ
What is the intuition behind Construct Quad Tree?
If every cell in a square has the same value, it's a single leaf. Otherwise split it into four equal quadrants and build each one recursively.
How do you solve Construct Quad Tree (LeetCode 427)?
1. Check whether all cells in the current n×n square match. 2. Yes → leaf with that value. 3. No → halve n and build top-left, top-right, bottom-left, bottom-right. 4. Combine into an internal node.
What is the time and space complexity of Construct Quad Tree?
Time: O(n² log n). Space: O(log n). (Accepted approach: Divide and conquer.)
What are common mistakes in Construct Quad Tree?
Checking uniformity by scanning at every level costs O(n² log n); prefix sums can make it O(n²).