Construct Quad Tree
LeetCode 427 · मध्यम · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू
Uniform square → leaf; else recurse on 4 quadrants.
मी Construct Quad Tree (LeetCode 427) असा सोडवला — Other पॅटर्नमधील एक मध्यम प्रॉब्लेम. खाली: intuition, टप्प्याटप्प्याने approach, माझा accepted Python कोड, animated test cases आणि 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.
मी कसा सोडवला
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 — वारंवार विचारले जाणारे प्रश्न
Construct Quad Tree मागील 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.
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.
Construct Quad Tree ची time आणि space complexity किती आहे?
Time: O(n² log n). Space: O(log n). (Accepted approach: Divide and conquer.)
Construct Quad Tree मध्ये सामान्य चुका कोणत्या?
Checking uniformity by scanning at every level costs O(n² log n); prefix sums can make it O(n²).