|
| 1 | +427\. Construct Quad Tree |
| 2 | + |
| 3 | +Medium |
| 4 | + |
| 5 | +Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent the `grid` with a Quad-Tree. |
| 6 | + |
| 7 | +Return _the root of the Quad-Tree_ representing the `grid`. |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes: |
| 12 | + |
| 13 | +* `val`: True if the node represents a grid of 1's or False if the node represents a grid of 0's. |
| 14 | +* `isLeaf`: True if the node is leaf node on the tree or False if the node has the four children. |
| 15 | + |
| 16 | +class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; } |
| 17 | + |
| 18 | +We can construct a Quad-Tree from a two-dimensional area using the following steps: |
| 19 | + |
| 20 | +1. 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. |
| 21 | +2. 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. |
| 22 | +3. Recurse for each of the children with the proper sub-grid. |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +If you want to know more about the Quad-Tree, you can refer to the [wiki](https://en.wikipedia.org/wiki/Quadtree). |
| 27 | + |
| 28 | +**Quad-Tree format:** |
| 29 | + |
| 30 | +The output represents the serialized format of a Quad-Tree using level order traversal, where `null` signifies a path terminator where no node exists below. |
| 31 | + |
| 32 | +It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list `[isLeaf, val]`. |
| 33 | + |
| 34 | +If the value of `isLeaf` or `val` is True we represent it as **1** in the list `[isLeaf, val]` and if the value of `isLeaf` or `val` is False we represent it as **0**. |
| 35 | + |
| 36 | +**Example 1:** |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +**Input:** grid = \[\[0,1\],\[1,0\]\] |
| 41 | + |
| 42 | +**Output:** \[\[0,1\],\[1,0\],\[1,1\],\[1,1\],\[1,0\]\] |
| 43 | + |
| 44 | +**Explanation:** |
| 45 | + |
| 46 | + The explanation of this example is shown below: |
| 47 | + Notice that 0 represnts False and 1 represents True in the photo representing the Quad-Tree. |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +**Example 2:** |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +**Input:** grid = \[\[1,1,1,1,0,0,0,0\],\[1,1,1,1,0,0,0,0\],\[1,1,1,1,1,1,1,1\],\[1,1,1,1,1,1,1,1\],\[1,1,1,1,0,0,0,0\],\[1,1,1,1,0,0,0,0\],\[1,1,1,1,0,0,0,0\],\[1,1,1,1,0,0,0,0\]\] |
| 56 | + |
| 57 | +**Output:** \[\[0,1\],\[1,1\],\[0,1\],\[1,1\],\[1,0\],null,null,null,null,\[1,0\],\[1,0\],\[1,1\],\[1,1\]\] |
| 58 | + |
| 59 | +**Explanation:** |
| 60 | + |
| 61 | + All values in the grid are not the same. We divide the grid into four sub-grids. |
| 62 | + The topLeft, bottomLeft and bottomRight each has the same value. |
| 63 | + The topRight have different values so we divide it into 4 sub-grids where each has the same value. |
| 64 | + Explanation is shown in the photo below: |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +**Constraints:** |
| 69 | + |
| 70 | +* `n == grid.length == grid[i].length` |
| 71 | +* <code>n == 2<sup>x</sup></code> where `0 <= x <= 6` |
0 commit comments