Prepin
Log in

Transform a matrix into a quad tree.

Approach / Explanation

The process of transforming a matrix into a quad tree involves splitting the matrix into four equal sections, or quadrants, and recursively applying this division until we reach a base case where all elements within a quadrant are identical. This issue is typically addressed using the divide and conquer approach, along with concepts from data structures. It's important to recognize that the matrix size must be a power of 2, meaning each dimension of the matrix is consistently a power of 2. This ensures a perfect quad tree structure without the need for zero-padding. If the matrix has a size of n , it can be partitioned into four submatrices of size n/2 x n/2 . The crucial points to consider for solving this problem are: Familiarize yourself with the quad tree data structure. In a quad tree, each node either holds a value if all of its children share the same value or subdivides its area into four quadrants: top-left, top-right, bottom-left, and bottom-right. Apply the divide-and-conquer method by recursively splitting the matrix into quadrants until the current section is uniform (all elements are identical), at which point a leaf node can be created to store that singular value.

Suggested Answer

The procedure for creating a quad tree from a matrix is as follows: class Node : def __init__ ( self , value , isLeaf , topLeft , topRight , bottomLeft , bottomRight ) : self . value = value self . isLeaf = isLeaf self . topLeft = topLeft self . topRight = topRight self . bottomLeft = bottomLeft self . bottomRight = bottomRight def construct ( matrix ) : N = len ( matrix ) if N == 1 : return Node ( matrix [ 0 ] [ 0 ] , True , None , None , None , None ) topLeft = construct ( [ row [ : N // 2 ] for row in matrix [ : N // 2 ] ] ) topRight = construct ( [ row [ N // 2 : ] for row in matrix [ : N // 2 ] ] ) bottomLeft = construct ( [ row [ : N // 2 ] for row in matrix [ N // 2 : ] ] ) bottomRight = construct ( [ row [ N // 2 : ] for row in matrix [ N // 2 : ] ] ) if topLeft . isLeaf and topRight . isLeaf and bottomLeft . isLeaf and bottomRight . isLeaf and topLeft . value == topRight . value == bottomLeft . value == bottomRight . value : return Node ( topLeft . value , True , None , None , None , None ) else : return Node ( '*' , False , topLeft , topRight , bottomLeft , bottomRight ) This Python script begins by defining the Node class, followed by the construct method. This method divides the matrix into four quadrants: top left, top right, bottom left, and bottom right. It then recursively calls itself with the new submatrices until it reaches the base case where the matrix dimensions are 1x1.

Alternative Answer

As an alternative approach, you can first transform the matrix into a binary format, which can then be easily converted into a quad-tree. However, this technique is primarily applicable to matrices containing binary values and may be overly complex for matrices with larger integer values: class Node { boolean val ; boolean isLeaf ; Node topLeft , topRight , bottomLeft , bottomRight ; } public Node construct ( int [ ] [ ] grid ) { int n = grid . length ; Node result = new Node ( ) ; result . isLeaf = isLeaf ( grid , 0 , 0 , n - 1 , n - 1 ) ; if ( result . isLeaf ) { result . val = grid [ 0 ] [ 0 ] == 1 ; } else { result . topLeft = construct ( subGrid ( grid , 0 , n / 2 , 0 , n / 2 ) ) ; result . topRight = construct ( subGrid ( grid , 0 , n / 2 , n / 2 , n ) ) ; result . bottomLeft = construct ( subGrid ( grid , n / 2 , n , 0 , n / 2 ) ) ; result . bottomRight = construct ( subGrid ( grid , n / 2 , n , n / 2 , n ) ) ; } return result ; } private boolean isLeaf ( int [ ] [ ] grid , int rowStart , int rowEnd , int colStart , int colEnd ) { int first = grid [ rowStart ] [ colStart ] ; for ( int i = rowStart ; i < rowEnd ; i ++ ) { for ( int j = colStart ; j < colEnd ; j ++ ) { if ( grid [ i ] [ j ] != first ) return false ; } } return true ; } private int [ ] [ ] subGrid ( int [ ] [ ] grid , int rowStart , int rowEnd , int colStart , int colEnd ) { int [ ] [ ] result = new int [ rowEnd - rowStart ] [ colEnd - colStart ] ; for ( int i = rowStart ; i < rowEnd ; i ++ ) { for ( int j = colStart ; j < colEnd ; j ++ ) { result [ i - rowStart ] [ j - colStart ] = grid [ i ] [ j ] ; } } return result ; } In this Java code, we begin by defining the Node class, followed by the construct method that accepts the grid matrix. If the matrix can be identified as a leaf node (with all elements being identical), that value is returned. Otherwise, the grid is divided into four sections, and each is constructed individually. The isLeaf method checks if a portion of the grid can be represented as a leaf node, while the subGrid method creates a subgrid from a specified section of the original grid.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Uber

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.