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