Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 427 · Other

Construct Quad Tree

LeetCode 427 · మధ్యస్థం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

Uniform square → leaf; else recurse on 4 quadrants.

మధ్యస్థంArrayDivide and ConquerTreeMatrix
LeetCode లో తెరవండి ↗

నేను 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. 1

    Check whether all cells in the current n×n square match.

  2. 2

    Yes → leaf with that value.

  3. 3

    No → halve n and build top-left, top-right, bottom-left, bottom-right.

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