|
11 | 11 |
|
12 | 12 | ## [427. Construct Quad Tree (Medium)](https://leetcode.com/problems/construct-quad-tree "建立四叉树")
|
13 | 13 |
|
14 |
| -<p>We want to use quad trees to store an <code>N x N</code> 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 <strong>until the values in the region it represents are all the same</strong>.</p> |
| 14 | +<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0's</code> and <code>1's</code> only. We want to represent the <code>grid</code> with a Quad-Tree.</p> |
15 | 15 |
|
16 |
| -<p>Each node has another two boolean attributes : <code>isLeaf</code> and <code>val</code>. <code>isLeaf</code> is true if and only if the node is a leaf node. The <code>val</code> attribute for a leaf node contains the value of the region it represents.</p> |
| 16 | +<p>Return <em>the root of the Quad-Tree</em> representing the <code>grid</code>.</p> |
17 | 17 |
|
18 |
| -<p>Your task is to use a quad tree to represent a given grid. The following example may help you understand the problem better:</p> |
| 18 | +<p>Notice that you can assign the value of a node to <strong>True</strong> or <strong>False</strong> when <code>isLeaf</code> is <strong>False</strong>, and both are <strong>accepted</strong> in the answer.</p> |
19 | 19 |
|
20 |
| -<p>Given the <code>8 x 8</code> grid below, we want to construct the corresponding quad tree:</p> |
| 20 | +<p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> |
21 | 21 |
|
22 |
| -<p><img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/01/962_grid.png" style="height:27%; max-height:300px; max-width:299px; width:27%" /></p> |
| 22 | +<ul> |
| 23 | + <li><code>val</code>: True if the node represents a grid of 1's or False if the node represents a grid of 0's. </li> |
| 24 | + <li><code>isLeaf</code>: True if the node is leaf node on the tree or False if the node has the four children.</li> |
| 25 | +</ul> |
23 | 26 |
|
24 |
| -<p>It can be divided according to the definition above:</p> |
| 27 | +<pre> |
| 28 | +class Node { |
| 29 | + public boolean val; |
| 30 | + public boolean isLeaf; |
| 31 | + public Node topLeft; |
| 32 | + public Node topRight; |
| 33 | + public Node bottomLeft; |
| 34 | + public Node bottomRight; |
| 35 | +}</pre> |
25 | 36 |
|
26 |
| -<p><img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/01/962_grid_divided.png" style="height:100%; max-height:300px; max-width:1107px; width:100%" /></p> |
| 37 | +<p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> |
| 38 | + |
| 39 | +<ol> |
| 40 | + <li>If the current grid has the same value (i.e all <code>1's</code> or all <code>0's</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> |
| 41 | + <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> |
| 42 | + <li>Recurse for each of the children with the proper sub-grid.</li> |
| 43 | +</ol> |
| 44 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> |
| 45 | +<p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> |
| 46 | + |
| 47 | +<p><strong>Quad-Tree format:</strong></p> |
| 48 | + |
| 49 | +<p>The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> |
| 50 | + |
| 51 | +<p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> |
| 52 | + |
| 53 | +<p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> |
27 | 54 |
|
28 | 55 | <p> </p>
|
| 56 | +<p><strong>Example 1:</strong></p> |
| 57 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> |
| 58 | +<pre> |
| 59 | +<strong>Input:</strong> grid = [[0,1],[1,0]] |
| 60 | +<strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] |
| 61 | +<strong>Explanation:</strong> The explanation of this example is shown below: |
| 62 | +Notice that 0 represnts False and 1 represents True in the photo representing the Quad-Tree. |
| 63 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> |
| 64 | +</pre> |
29 | 65 |
|
30 |
| -<p>The corresponding quad tree should be as following, where each node is represented as a <code>(isLeaf, val)</code> pair.</p> |
| 66 | +<p><strong>Example 2:</strong></p> |
31 | 67 |
|
32 |
| -<p>For the non-leaf nodes, <code>val</code> can be arbitrary, so it is represented as <code>*</code>.</p> |
| 68 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> |
33 | 69 |
|
34 |
| -<p><img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/01/962_quad_tree.png" style="height:100%; max-height:300px; max-width:836px; width:100%" /></p> |
| 70 | +<pre> |
| 71 | +<strong>Input:</strong> 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]] |
| 72 | +<strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] |
| 73 | +<strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. |
| 74 | +The topLeft, bottomLeft and bottomRight each has the same value. |
| 75 | +The topRight have different values so we divide it into 4 sub-grids where each has the same value. |
| 76 | +Explanation is shown in the photo below: |
| 77 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> |
| 78 | +</pre> |
35 | 79 |
|
36 |
| -<p><strong>Note:</strong></p> |
| 80 | +<p><strong>Example 3:</strong></p> |
37 | 81 |
|
38 |
| -<ol> |
39 |
| - <li><code>N</code> is less than <code>1000</code> and guaranteened to be a power of 2.</li> |
40 |
| - <li>If you want to know more about the quad tree, you can refer to its <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</li> |
41 |
| -</ol> |
| 82 | +<pre> |
| 83 | +<strong>Input:</strong> grid = [[1,1],[1,1]] |
| 84 | +<strong>Output:</strong> [[1,1]] |
| 85 | +</pre> |
| 86 | + |
| 87 | +<p><strong>Example 4:</strong></p> |
| 88 | + |
| 89 | +<pre> |
| 90 | +<strong>Input:</strong> grid = [[0]] |
| 91 | +<strong>Output:</strong> [[1,0]] |
| 92 | +</pre> |
| 93 | + |
| 94 | +<p><strong>Example 5:</strong></p> |
| 95 | + |
| 96 | +<pre> |
| 97 | +<strong>Input:</strong> grid = [[1,1,0,0],[1,1,0,0],[0,0,1,1],[0,0,1,1]] |
| 98 | +<strong>Output:</strong> [[0,1],[1,1],[1,0],[1,0],[1,1]] |
| 99 | +</pre> |
| 100 | + |
| 101 | +<p> </p> |
| 102 | +<p><strong>Constraints:</strong></p> |
| 103 | + |
| 104 | +<ul> |
| 105 | + <li><code>n == grid.length == grid[i].length</code></li> |
| 106 | + <li><code>n == 2^x</code> where <code>0 <= x <= 6</code></li> |
| 107 | +</ul> |
0 commit comments