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²).