|
| 1 | +# LeetCode 427. Construct Quad Tree |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +We want to use quad trees to store an N x N 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 until the values in the region it represents are all the same. |
| 6 | + |
| 7 | +Each node has another two boolean attributes : isLeaf and val. isLeaf is true if and only if the node is a leaf node. The val attribute for a leaf node contains the value of the region it represents. |
| 8 | + |
| 9 | +Your task is to use a quad tree to represent a given grid. The following example may help you understand the problem better: |
| 10 | + |
| 11 | +Given the 8 x 8 grid below, we want to construct the corresponding quad tree: |
| 12 | + |
| 13 | +It can be divided according to the definition above: |
| 14 | + |
| 15 | +The corresponding quad tree should be as following, where each node is represented as a (isLeaf, val) pair. |
| 16 | + |
| 17 | +For the non-leaf nodes, val can be arbitrary, so it is represented as *. |
| 18 | + |
| 19 | +Note: |
| 20 | + |
| 21 | +N is less than 1000 and guaranteened to be a power of 2. |
| 22 | +If you want to know more about the quad tree, you can refer to its wiki. |
| 23 | + |
| 24 | +## 描述 |
| 25 | + |
| 26 | +我们想要使用一棵四叉树来储存一个 N x N 的布尔值网络。网络中每一格的值只会是真或假。树的根结点代表整个网络。对于每个结点, 它将被分等成四个孩子结点直到这个区域内的值都是相同的. |
| 27 | + |
| 28 | +每个结点还有另外两个布尔变量: isLeaf 和 val。isLeaf 当这个节点是一个叶子结点时为真。val 变量储存叶子结点所代表的区域的值。 |
| 29 | + |
| 30 | +你的任务是使用一个四叉树表示给定的网络。下面的例子将有助于你理解这个问题: |
| 31 | + |
| 32 | +给定下面这个8 x 8 网络,我们将这样建立一个对应的四叉树: |
| 33 | + |
| 34 | +由上文的定义,它能被这样分割: |
| 35 | + |
| 36 | +对应的四叉树应该像下面这样,每个结点由一对 (isLeaf, val) 所代表. |
| 37 | + |
| 38 | +对于非叶子结点,val 可以是任意的,所以使用 * 代替。 |
| 39 | + |
| 40 | +提示: |
| 41 | + |
| 42 | +N 将小于 1000 且确保是 2 的整次幂。 |
| 43 | +如果你想了解更多关于四叉树的知识,你可以参考这个 wiki 页面。 |
| 44 | + |
| 45 | +来源:力扣(LeetCode) |
| 46 | +链接:https://leetcode-cn.com/problems/construct-quad-tree |
| 47 | +著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 |
| 48 | +### 思路 |
| 49 | + |
| 50 | +```py |
| 51 | +# -*- coding: utf-8 -*- |
| 52 | +# @Author: 何睿 |
| 53 | +# @Create Date: 2019-12-14 10:29:10 |
| 54 | +# @Last Modified by: 何睿 |
| 55 | +# @Last Modified time: 2019-12-14 10:39:51 |
| 56 | + |
| 57 | + |
| 58 | +class Node: |
| 59 | + def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight): |
| 60 | + self.val = val |
| 61 | + self.isLeaf = isLeaf |
| 62 | + self.topLeft = topLeft |
| 63 | + self.topRight = topRight |
| 64 | + self.bottomLeft = bottomLeft |
| 65 | + self.bottomRight = bottomRight |
| 66 | + |
| 67 | + |
| 68 | +class Solution: |
| 69 | + def construct(self, grid): |
| 70 | + return self.dfs(grid, 0, 0, len(grid)) |
| 71 | + |
| 72 | + def dfs(self, grid, h, w, N): |
| 73 | + |
| 74 | + # 求取当前方格内的和 |
| 75 | + total = sum([grid[h + i][w + j] for i in range(N) for j in range(N)]) |
| 76 | + |
| 77 | + # 如果方格内所有元素都是0,构造 False 节点 |
| 78 | + if total == 0: |
| 79 | + return Node(False, True, None, None, None, None) |
| 80 | + # 如果全部为 1,构造 True节点 |
| 81 | + elif total == N * N: |
| 82 | + return Node(True, True, None, None, None, None) |
| 83 | + # 否则拆分方格子,分别构造 |
| 84 | + else: |
| 85 | + root = Node('*', False, None, None, None, None) |
| 86 | + n = N // 2 |
| 87 | + root.topLeft = self.dfs(grid, h, w, n) |
| 88 | + root.topRight = self.dfs(grid, h, w + n, n) |
| 89 | + root.bottomLeft = self.dfs(grid, h + n, w, n) |
| 90 | + root.bottomRight = self.dfs(grid, h + n, w + n, n) |
| 91 | + return root |
| 92 | + |
| 93 | +``` |
| 94 | +源代码文件在 [这里](https://github.com/ruicore/Algorithm/blob/master/LeetCode/2019-12-14-427-Construct-Quad-Tree.py) 。 |
| 95 | +©本文首发于 何睿的博客 ,欢迎转载,转载需保留 [文章来源](https://ruicore.cn/leetcode-427-construct-quad-tree/) ,作者信息和本声明. |
0 commit comments