{"id":3211,"date":"2018-07-17T22:59:36","date_gmt":"2018-07-18T05:59:36","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3211"},"modified":"2018-07-17T23:14:12","modified_gmt":"2018-07-18T06:14:12","slug":"leetcode-427-construct-quad-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-427-construct-quad-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 427. Construct Quad Tree"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>We want to use quad trees to store an\u00a0<code>N x N<\/code>\u00a0boolean 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\u00a0<strong>until the values in the region it represents are all the same<\/strong>.<\/p>\n<p>Each node has another two boolean attributes :\u00a0<code>isLeaf<\/code>\u00a0and\u00a0<code>val<\/code>.\u00a0<code>isLeaf<\/code>\u00a0is true if and only if the node is a leaf node. The\u00a0<code>val<\/code>\u00a0attribute for a leaf node contains the value of the region it represents.<\/p>\n<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>\n<p>Given the\u00a0<code>8 x 8<\/code>\u00a0grid below, we want to construct the corresponding quad tree:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/s3-lc-upload.s3.amazonaws.com\/uploads\/2018\/02\/01\/962_grid.png\" alt=\"\" \/><\/p>\n<p>It can be divided according to the definition above:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/s3-lc-upload.s3.amazonaws.com\/uploads\/2018\/02\/01\/962_grid_divided.png\" alt=\"\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The corresponding quad tree should be as following, where each node is represented as a\u00a0<code>(isLeaf, val)<\/code>pair.<\/p>\n<p>For the non-leaf\u00a0nodes,\u00a0<code>val<\/code>\u00a0can be arbitrary, so it is represented as\u00a0<code>*<\/code>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/s3-lc-upload.s3.amazonaws.com\/uploads\/2018\/02\/01\/962_quad_tree.png\" alt=\"\" \/><\/p>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>N<\/code>\u00a0is less than\u00a0<code>1000<\/code>\u00a0and guaranteened to be a power of 2.<\/li>\n<li>If you want to know more about the quad tree, you can refer to its\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Quadtree\">wiki<\/a>.<\/li>\n<\/ol>\n<h1><strong>Solution: Recursion<\/strong><\/h1>\n<p>Time complexity: O(n^2*logn)<\/p>\n<p>Space complexity: O(n^2)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 44 ms\r\nclass Solution {\r\npublic:\r\n  Node* construct(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {    \r\n    return construct(grid, 0, 0, grid.size());\r\n  }\r\nprivate:\r\n  Node* construct(const vector&lt;vector&lt;int&gt;&gt;&amp; grid, int x, int y, int n) {\r\n    if (n == 0) return nullptr;    \r\n    bool all_zeros = true;\r\n    bool all_ones = true;\r\n    for (int i = y; i &lt; y + n; ++i)\r\n      for (int j = x; j &lt; x + n; ++j)\r\n        if (grid[i][j] == 0)\r\n          all_ones = false;\r\n        else\r\n          all_zeros = false;\r\n    if (all_zeros || all_ones)\r\n      return new Node(all_ones, true, nullptr, nullptr, nullptr, nullptr);\r\n    \r\n    return new Node(true, false,\r\n                    construct(grid, x,       y,       n\/2),  \/\/ topLeft\r\n                    construct(grid, x + n\/2, y,       n\/2),  \/\/ topRight\r\n                    construct(grid, x,       y + n\/2, n\/2),  \/\/ bottomLeft\r\n                    construct(grid, x + n\/2, y + n\/2, n\/2)); \/\/ bottomRight\r\n  }\r\n};<\/pre>\n<p>V2<\/p>\n<p>Time complexity: O(n^2)<\/p>\n<p>Space complexity: O(n^2)<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 56 ms\r\nclass Solution {\r\npublic:\r\n  Node* construct(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {    \r\n    return construct(grid, 0, 0, grid.size());\r\n  }\r\nprivate:\r\n  Node* construct(const vector&lt;vector&lt;int&gt;&gt;&amp; grid, int x, int y, int n) {\r\n    if (n == 0) return nullptr;    \r\n    if (n == 1) return new Node(grid[y][x], true, nullptr, nullptr, nullptr, nullptr);\r\n    \r\n    auto tl = construct(grid, x,       y,       n\/2);   \/\/ topLeft\r\n    auto tr = construct(grid, x + n\/2, y,       n\/2);   \/\/ topRight\r\n    auto bl = construct(grid, x,       y + n\/2, n\/2);   \/\/ bottomLeft\r\n    auto br = construct(grid, x + n\/2, y + n\/2, n\/2);   \/\/ bottomRight\r\n    \r\n    if (tl-&gt;isLeaf &amp;&amp; tr-&gt;isLeaf &amp;&amp; bl-&gt;isLeaf &amp;&amp; br-&gt;isLeaf \r\n    &amp;&amp; tl-&gt;val == tr-&gt;val &amp;&amp; tl-&gt;val == bl-&gt;val &amp;&amp; tl-&gt;val == br-&gt;val) {\r\n      auto root = new Node(tl-&gt;val, true, nullptr, nullptr, nullptr, nullptr);\r\n      delete tl;\r\n      delete tr;\r\n      delete bl;\r\n      delete br;\r\n      return root;\r\n    }\r\n    \r\n    return new Node(true, false, tl, tr, bl, br);                    \r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem We want to use quad trees to store an\u00a0N x N\u00a0boolean grid. Each cell in the grid can only be true or false. The&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[127],"tags":[222,341,17],"class_list":["post-3211","post","type-post","status-publish","format-standard","hentry","category-geometry","tag-easy","tag-quad-tree","tag-recursion","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3211"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3211\/revisions"}],"predecessor-version":[{"id":3216,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3211\/revisions\/3216"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}