Class Solution

java.lang.Object
g0401_0500.s0427_construct_quad_tree.Solution

public class Solution extends Object
427 - Construct Quad Tree.<p>Medium</p> <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> <p>Return <em>the root of the Quad-Tree</em> representing the <code>grid</code>.</p> <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> <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> <ul> <li><code>val</code>: True if the node represents a grid of 1&rsquo;s or False if the node represents a grid of 0&rsquo;s.</li> <li><code>isLeaf</code>: True if the node is leaf node on the tree or False if the node has the four children.</li> </ul> <pre><code> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; } </code></pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <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> <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> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <p><img src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" alt="" /></p> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree" target="_top">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <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> <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> <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> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" alt="" /></p> <p><strong>Input:</strong> grid = [[0,1],[1,0]]</p> <p><strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]]</p> <p><strong>Explanation:</strong></p> <pre><code> The explanation of this example is shown below: Notice that 0 represnts False and 1 represents True in the photo representing the Quad-Tree. </code></pre> <p><img src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" alt="" /></p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" alt="" /></p> <p><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]]</p> <p><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]]</p> <p><strong>Explanation:</strong></p> <pre><code> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: </code></pre> <p><img src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" alt="" /></p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 <= x <= 6</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • construct

      public Node construct(int[][] grid)