Question

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: Imgur

The corresponding quad tree should be as following, where each node is represented as a (isLeaf, val) pair. Imgur 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.

Solutions

  • Method 1: Recursion from bottom to top.
      /*
      // 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 {
          private int[][] grid;
          public Node construct(int[][] grid) {
              this.grid = grid;
              int len = grid.length;
              return dfs(0, grid.length - 1, 0, grid.length - 1);
          }
          private Node dfs(int xs, int xe, int ys, int ye){
              Node node = new Node();
              if(xs == xe){
                  node.val = (grid[xs][ys] == 1);
                  node.isLeaf = true;
                  return node;
              }
              Node upLeft = dfs(xs, xs + (xe - xs) / 2, ys, ys + (ye - ys) / 2);
              Node upRight = dfs(xs, xs + (xe - xs) / 2, ys + (ye - ys) / 2 + 1, ye);
              Node bottomLeft = dfs(xs + (xe - xs) / 2 + 1, xe, ys, ys + (ye - ys) / 2);
              Node bottomRight = dfs(xs + (xe - xs) / 2 + 1, xe, ys + (ye - ys) / 2 + 1, ye);
              if(bottomLeft.isLeaf && bottomRight.isLeaf && upLeft.isLeaf && upRight.isLeaf
                && bottomLeft.val == bottomRight.val && bottomRight.val == upLeft.val && upLeft.val == upRight.val){
                  node.isLeaf = true;
                  node.val = bottomLeft.val;
              }else{
                  node.bottomLeft = bottomLeft;
                  node.bottomRight = bottomRight;
                  node.topLeft = upLeft;
                  node.topRight = upRight;
              }
              return node;
          }
      }