|
| 1 | +/** |
| 2 | +* We want to use quad trees to store an N x N boolean grid. Each cell in the |
| 3 | +* grid can only be true or false. The root node represents the whole grid. For |
| 4 | +* each node, it will be subdivided into four children nodes until the values |
| 5 | +* in the region it represents are all the same. |
| 6 | +* |
| 7 | +* Each node has another two boolean attributes : isLeaf and val. isLeaf is |
| 8 | +* true if and only if the node is a leaf node. The val attribute for a leaf |
| 9 | +* node contains the value of the region it represents. |
| 10 | +* |
| 11 | +* Your task is to use a quad tree to represent a given grid. The following |
| 12 | +* example may help you understand the problem better: |
| 13 | +* |
| 14 | +* Given the 8 x 8 grid below, we want to construct the corresponding quad tree: |
| 15 | +* https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/01/962_grid.png |
| 16 | +* |
| 17 | +* It can be divided according to the definition above: |
| 18 | +* https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/01/962_grid_divided.png |
| 19 | +* |
| 20 | +* The corresponding quad tree should be as following, where each node is |
| 21 | +* represented as a (isLeaf, val) pair. |
| 22 | +* |
| 23 | +* For the non-leaf nodes, val can be arbitrary, so it is represented as *. |
| 24 | +* https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/01/962_quad_tree.png |
| 25 | +* |
| 26 | +* Note: |
| 27 | +* N is less than 1000 and guaranteened to be a power of 2. |
| 28 | +* If you want to know more about the quad tree, you can refer to its wiki. |
| 29 | +*/ |
| 30 | + |
| 31 | +/* |
| 32 | +// Definition for a QuadTree node. |
| 33 | +class Node { |
| 34 | +public boolean val; |
| 35 | +public boolean isLeaf; |
| 36 | +public Node topLeft; |
| 37 | +public Node topRight; |
| 38 | +public Node bottomLeft; |
| 39 | +public Node bottomRight; |
| 40 | +
|
| 41 | +public Node() {} |
| 42 | +
|
| 43 | +public Node(boolean _val,boolean _isLeaf,Node _topLeft,Node _topRight,Node _bottomLeft,Node _bottomRight) { |
| 44 | +val = _val; |
| 45 | +isLeaf = _isLeaf; |
| 46 | +topLeft = _topLeft; |
| 47 | +topRight = _topRight; |
| 48 | +bottomLeft = _bottomLeft; |
| 49 | +bottomRight = _bottomRight; |
| 50 | +} |
| 51 | +}; |
| 52 | +*/ |
| 53 | + |
| 54 | +public class ConstructQuadTree427 { |
| 55 | +public Node construct(int[][] grid) { |
| 56 | +if (grid == null) return null; |
| 57 | +int N = grid.length; |
| 58 | +return construct(grid, 0, N-1, 0, N-1); |
| 59 | +} |
| 60 | + |
| 61 | +public Node construct(int[][] grid, int xi, int xj, int yi, int yj) { |
| 62 | +if (xi > xj) return null; |
| 63 | +Node res = new Node(); |
| 64 | +if (xi == xj) { |
| 65 | +res.isLeaf = true; |
| 66 | +res.val = grid[xi][yi] == 1; |
| 67 | +return res; |
| 68 | +} |
| 69 | + |
| 70 | +int xm = (xi + xj) / 2; |
| 71 | +int ym = (yi + yj) / 2; |
| 72 | +res.topLeft = construct(grid, xi, xm, yi, ym); |
| 73 | +res.topRight = construct(grid, xi, xm, ym+1, yj); |
| 74 | +res.bottomLeft = construct(grid, xm+1, xj, yi, ym); |
| 75 | +res.bottomRight = construct(grid, xm+1, xj, ym+1, yj); |
| 76 | + |
| 77 | +if (allLeaves(res) && allSame(res)) { |
| 78 | +res.isLeaf = true; |
| 79 | +res.val = res.topLeft.val; |
| 80 | +res.topLeft = null; |
| 81 | +res.topRight = null; |
| 82 | +res.bottomLeft = null; |
| 83 | +res.bottomRight = null; |
| 84 | +} |
| 85 | +return res; |
| 86 | +} |
| 87 | + |
| 88 | +private boolean allLeaves(Node n) { |
| 89 | +return n.topLeft.isLeaf && |
| 90 | +n.topRight.isLeaf && |
| 91 | +n.bottomLeft.isLeaf && |
| 92 | +n.bottomRight.isLeaf; |
| 93 | +} |
| 94 | + |
| 95 | +private boolean allSame(Node n) { |
| 96 | +return (n.topLeft.val == n.topRight.val) && |
| 97 | +(n.bottomLeft.val == n.bottomRight.val) && |
| 98 | +(n.topLeft.val == n.bottomLeft.val); |
| 99 | +} |
| 100 | + |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
0 commit comments