Formatted question description: https://leetcode.ca/all/427.html

427. Construct Quad Tree (Easy)

We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or false. The root node represents the whole grid. For each node, it will be subdivided into four children nodes until the values in the region it represents are all the same.

Each node has another two boolean attributes : isLeaf and val. isLeaf is true if and only if the node is a leaf node. The val attribute for a leaf node contains the value of the region it represents.

Your task is to use a quad tree to represent a given grid. The following example may help you understand the problem better:

Given the 8 x 8 grid below, we want to construct the corresponding quad tree:

It can be divided according to the definition above:

 

The corresponding quad tree should be as following, where each node is represented as a (isLeaf, val) pair.

For the non-leaf nodes, val can be arbitrary, so it is represented as *.

Note:

  1. N is less than 1000 and guaranteened to be a power of 2.
  2. If you want to know more about the quad tree, you can refer to its wiki.

Companies:
Uber

Solution 1.

// OJ: https://leetcode.com/problems/construct-quad-tree/

// Time: O(logN * N^2)
// Space: O(logN)
class Solution {
private:
    Node *dfs(vector<vector<int>> &grid, int x, int y, int N) {
        bool same = true;
        for (int i = 0; i < N && same; ++i) {
            for (int j = 0; j < N && same; ++j) {
                same = grid[x][y] == grid[x + i][y + j];
            }
        }
        if (same) return new Node(grid[x][y], true, NULL, NULL, NULL, NULL);
        return new Node(true, false,
                             dfs(grid, x, y, N / 2),
                             dfs(grid, x, y + N / 2, N / 2),
                             dfs(grid, x + N / 2, y, N / 2),
                             dfs(grid, x + N / 2, y + N / 2, N / 2));
    }
public:
    Node* construct(vector<vector<int>>& grid) {
        return dfs(grid, 0, 0, grid.size());
    }
};

Java

/*
// Definition for a QuadTree node.
class Node {
    public boolean val;
    public boolean isLeaf;
    public Node topLeft;
    public Node topRight;
    public Node bottomLeft;
    public Node bottomRight;

    public Node() {}

    public Node(boolean _val,boolean _isLeaf,Node _topLeft,Node _topRight,Node _bottomLeft,Node _bottomRight) {
        val = _val;
        isLeaf = _isLeaf;
        topLeft = _topLeft;
        topRight = _topRight;
        bottomLeft = _bottomLeft;
        bottomRight = _bottomRight;
    }
};
*/
class Solution {
    public Node construct(int[][] grid) {
        if (grid == null || grid.length == 0 || grid[0].length == 0)
            return null;
        int length = grid.length;
        if (length == 1) {
            int gridVal = grid[0][0];
            boolean val = gridVal == 1 ? true : false;
            Node root = new Node(val, true, null, null, null, null);
            return root;
        } else {
            int halfLength = length / 2;
            int[][] topLeftGrid = new int[halfLength][halfLength];
            int[][] topRightGrid = new int[halfLength][halfLength];
            int[][] bottomLeftGrid = new int[halfLength][halfLength];
            int[][] bottomRightGrid = new int[halfLength][halfLength];
            for (int i = 0; i < halfLength; i++) {
                for (int j = 0; j < halfLength; j++) {
                    topLeftGrid[i][j] = grid[i][j];
                    topRightGrid[i][j] = grid[i][j + halfLength];
                    bottomLeftGrid[i][j] = grid[i + halfLength][j];
                    bottomRightGrid[i][j] = grid[i + halfLength][j + halfLength];
                }
            }
            Node topLeft = construct(topLeftGrid);
            Node topRight = construct(topRightGrid);
            Node bottomLeft = construct(bottomLeftGrid);
            Node bottomRight = construct(bottomRightGrid);
            if (topLeft.isLeaf && topRight.isLeaf && bottomLeft.isLeaf && bottomRight.isLeaf && topLeft.val == topRight.val && topLeft.val == bottomLeft.val && topLeft.val == bottomRight.val) {
                Node root = new Node(topLeft.val, true, null, null, null, null);
                return root;
            } else {
                boolean val = topLeft.val || topRight.val || bottomLeft.val || bottomRight.val;
                Node root = new Node(val, false, topLeft, topRight, bottomLeft, bottomRight);
                return root;
            }
        }
    }
}

All Problems

All Solutions