Leetcode Construct Quad Tree problem solution

In this Leetcode Construct Quad Tree problem solution we have given a n * n matrix grid of 0’s and 1’s only. We want to represent the grid with a Quad-Tree.

Return the root of the Quad-Tree representing the grid.

Notice that you can assign the value of a node to True or False when isLeaf is False, and both are accepted in the answer.

A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:

val: True if the node represents a grid of 1’s or False if the node represents a grid of 0’s. 

isLeaf: True if the node is leaf node on the tree or False if the node has the four children.

class Node {

    public boolean val;

    public boolean isLeaf;

    public Node topLeft;

    public Node topRight;

    public Node bottomLeft;

    public Node bottomRight;

}

We can construct a Quad-Tree from a two-dimensional area using the following steps:

If the current grid has the same value (i.e all 1’s or all 0’s) set isLeaf True and set val to the value of the grid and set the four children to Null and stop.

If the current grid has different values, set isLeaf to False and set val to any value and divide the current grid into four sub-grids as shown in the photo.

Recurse for each of the children with the proper sub-grid.

Leetcode Construct Quad Tree problem solution

Problem solution in Python.

class Solution:
    def construct(self, grid: List[List[int]]) -> 'Node':
        def process(r1, r2, c1, c2):
            cnt0 = 0
            cnt1 = 0
            for i in range(r1, r2):
                for j in range(c1, c2):
                    if grid[i][j] == 0:
                        cnt0 += 1
                    else:
                        cnt1 += 1
            if cnt0 == 0:
                node = Node(1, True)
                return node
            if cnt1 == 0:
                node = Node(0, True)
                return node
            node = Node(0, False)
            node.topLeft = process(r1, (r1 + r2)//2, c1, (c1+c2)//2)
            node.topRight = process(r1, (r1 + r2)//2, (c1+c2)//2, c2)
            node.bottomLeft = process((r1 + r2)//2, r2, c1, (c1+c2)//2)
            node.bottomRight = process((r1 + r2)//2, r2, (c1+c2)//2, c2)
            return node
        if len(grid) == 0:
            return None
        return process(0, len(grid), 0, len(grid))

Problem solution in Java.

class Solution {
    public Node construct(int[][] grid) {
        return construct(grid, 0, 0, grid.length);
    }
    
    public Node construct(int[][] grid, int x, int y, int size) {
        if(size == 0) {
            return null;
        }
        Node node = new Node();
        if(gridHasSameValues(grid, x, y, size) != -1) {
            node.val = gridHasSameValues(grid, x, y, size) == 0 ? false : true;
            node.isLeaf = true;
            return node;
        }
        node.topLeft = construct(grid, x, y, size/2);
        node.topRight = construct(grid, x, y+size/2, size/2);
        node.bottomLeft = construct(grid, x+size/2, y, size/2);
        node.bottomRight = construct(grid, x+size/2, y+size/2, size/2);
        return node;
    }
    
    public int gridHasSameValues(int[][] grid, int x, int y, int size) {
        int value = grid[x][y];
        for(int i = x; i<x+size; i++) {
            for(int j = y; j < y+size; j++) {
                if(grid[i][j] != value) {
                    value = -1;
                    break;
                }
            }
        }
        return value;
    }
}

Problem solution in C++.

public Node construct(int[][] grid) {
        return construct(grid, 0, 0, grid.length);
    }
    
    public Node construct(int[][] grid, int rStart, int cStart, int size) {        
        Node node = new Node();        
        if (allSame(grid, rStart, cStart, size)) {
            node.isLeaf = true;
            node.val = (grid[rStart][cStart] == 1);
            return node;
        } else {
            node.topLeft = construct(grid, rStart, cStart, size/2);
            node.topRight = construct(grid, rStart, cStart + size/2, size/2);
            node.bottomLeft = construct(grid, rStart + size/2, cStart, size/2);
            node.bottomRight = construct(grid, rStart + size/2, cStart + size/2, size/2);
        }
        return node;
    }
    
    public boolean allSame(int[][] grid, int rStart, int cStart, int size) {
        int tmp = grid[rStart][cStart];
        for (int r=rStart; r<rStart+size; r++) {
            for (int c=cStart; c<cStart+size; c++) {
                if (grid[r][c] != tmp) {
                    return false;
                }
            }
        }
        return true;
    }