作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


目录

  • 题目描述
  • 题目大意
  • 解题方法
  • 日期

题目地址:https://leetcode.com/problems/construct-quad-tree/description/

题目描述

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.

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.

Your task is to use a quad tree to represent a given grid. The following example may help you understand the problem better:

Given the 8 x 8 grid below, we want to construct the corresponding quad tree:

It can be divided according to the definition above:

The corresponding quad tree should be as following, where each node is represented as a (isLeaf, val) pair.

For the non-leaf nodes, val can be arbitrary, so it is represented as *.

Note:

  1. N is less than 1000 and guaranteened to be a power of 2.
  2. If you want to know more about the quad tree, you can refer to its wiki.

题目大意

题目很长,但是不要害怕。

题目所说的四分树,其实就是用来表示矩阵数据的一种数据结构。

把一个边长为 2 的幂的正方形均分成 4 块,然后再均分到不能均分为止即为叶子节点。

树的节点分成两种:一种是叶子节点(矩阵内所有的取值一样),一种不是叶子节点(矩阵内的取值不一样,需要继续细分)。

具体地建立四叉树的过程,可以参考我画的图进行理解:




解题方法

首先,这种结构就是「树」,肯定使用递归求解。重要的是如何判断此树结构如何判断叶子节点、val

所以定义了一个新的函数isQuadTree

  • 如果一个正方形中所有的数字都是 1,则 val 是 True;
  • 如果一个正方形中所有的数字都是 0,则 val 是 False。
  • 否则,说明格子里的值不都是相同的,返回 None.

判断 leaf 的方法是看看格子里的所有的值是不是相同的,如果全是 1 或者 0 那么就是 leaf,否则就不是 leaf

其他的难点就在把正方形进行切分成四块了,这个不是难点。我用的是蠢方法,使用额外空间,把 4 个小区域都复制了一份出来。

"""
# Definition for a QuadTree node.
class Node:def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):self.val = valself.isLeaf = isLeafself.topLeft = topLeftself.topRight = topRightself.bottomLeft = bottomLeftself.bottomRight = bottomRight
"""
class Solution:def construct(self, grid):""":type grid: List[List[int]]:rtype: Node"""isLeaf = self.isQuadTree(grid)_len = len(grid)if isLeaf == None:mid = _len // 2topLeftGrid = [[grid[i][j] for j in range(mid)] for i in range(mid)]topRightGrid = [[grid[i][j] for j in range(mid, _len)] for i in range(mid)]bottomLeftGrid = [[grid[i][j] for j in range(mid)] for i in range(mid, _len)]bottomRightGrid = [[grid[i][j] for j in range(mid, _len)] for i in range(mid, _len)]node = Node(True, False, self.construct(topLeftGrid), self.construct(topRightGrid), self.construct(bottomLeftGrid), self.construct(bottomRightGrid))elif isLeaf == False:node = Node(False, True, None, None, None, None)else:node = Node(True, True, None, None, None, None)return nodedef isQuadTree(self, grid):_len = len(grid)_sum = 0for i in range(_len):_sum += sum(grid[i])if _sum == _len ** 2:return Trueelif _sum == 0:return Falseelse:return None

二刷,换了一种写法,也是递归。

"""
# Definition for a QuadTree node.
class Node(object):def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):self.val = valself.isLeaf = isLeafself.topLeft = topLeftself.topRight = topRightself.bottomLeft = bottomLeftself.bottomRight = bottomRight
"""
class Solution(object):def construct(self, grid):""":type grid: List[List[int]]:rtype: Node"""N = len(grid)if N == 1:return Node(grid[0][0] == 1, True, None, None, None, None)topLeftSum = sum([grid[i][j] for i in range(N/2) for j in range(N/2)])topRightSum = sum([grid[i][j] for i in range(N/2) for j in range(N/2, N)])bottomLeftSum = sum([grid[i][j] for i in range(N/2, N) for j in range(N/2)])bottomRightSum = sum(grid[i][j] for i in range(N/2, N) for j in range(N/2, N))node = Node(False, False, None, None, None, None)if topLeftSum == topRightSum == bottomLeftSum == bottomRightSum:if topLeftSum == 0:node.isLeaf = Truenode.val = Falseelif topLeftSum == (N / 2) ** 2:node.isLeaf = Truenode.val = Trueif node.isLeaf:return nodenode.val = Truenode.topLeft = self.construct([[grid[i][j] for j in range(N/2)] for i in range(N/2)])node.topRight = self.construct([[grid[i][j] for j in range(N/2, N)] for i in range(N/2)])node.bottomLeft = self.construct([[grid[i][j] for j in range(N/2)] for i in range(N/2, N)])node.bottomRight = self.construct([[grid[i][j] for j in range(N/2, N)] for i in range(N/2, N)])return node

日期

2018 年 8 月 19 日 —— 天阴阴,地潮潮,这种天气真舒服
2018 年 11 月 13 日 —— 时间有点快

【LeetCode】427. Construct Quad Tree 建立四叉树相关推荐

  1. LeetCode #427 - Construct Quad Tree

    题目描述: We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be tr ...

  2. leetcode 427. Construct Quad Tree | 427. 建立四叉树(分治法)

    题目 https://leetcode.com/problems/construct-quad-tree/ 题解 /* // Definition for a QuadTree node. class ...

  3. leetcode 427. Construct Quad Tree(构建四叉树)

    刚看到题的时候是懵的,这也太长了.到底是要表达什么呢. 不妨把这个矩阵看成一个正方形的图片,想象你在处理图片,从整体逐步到局部. 刚开始看一整张图片,如果是全0或全1,这个就是叶子节点,怎么表达叶子节 ...

  4. 427. Construct Quad Tree

    1,题目要求 We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be t ...

  5. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++...

    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++ Given preo ...

  6. LeetCode: 106. Construct Binary Tree from Inorder and Postorder Traversal

    题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...

  7. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal

    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal Solution1:我的答案 仿照105题写的答案 / ...

  8. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 本博客转载自:http://www.cnblogs.co ...

  9. quadtree java_LeetCode算法题-Construct Quad Tree(Java实现)

    这是悦乐书的第224次更新,第237篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第91题(顺位题号是427).我们想使用四叉树来存储N×N布尔网格.网格中的每个单元格只 ...

最新文章

  1. es6 ... 添加属性_如何在10分钟内免费将HTTPS添加到您的网站,以及为什么您现在不止需要这样做......
  2. XSS的原理分析与解剖
  3. Visual Studio 2008 使用小技巧
  4. 比较难理解的知识汇集
  5. POJ 1182 食物链 (并查集解法)(详细注释)
  6. 爬楼梯—leetcode70
  7. java设计模式之设计原则⑤迪米特原则
  8. java web filter 之一 基础实现
  9. linux内核zfs,Linus Torvalds 不建议使用 ZFS On Linux
  10. Atlas中间件实现Mysql读写分离
  11. 在 Kotlin 序列化中使用 DataStore
  12. 计算机设计大赛山东,第十届中国大学生计算机设计大赛山东赛区颁奖典礼在我校举办...
  13. Shell脚本速查手册
  14. dpdk对虚拟化支持研究
  15. juniper SRX55 简单配置
  16. PYNQ-overlay
  17. 安卓开发者的 17 年总结
  18. 基于vue-cli搭建的脚手架中的一些相关配置(转载)
  19. 市级政务云平台建设与运营解决方案
  20. 基于复合粒子群优化的模糊神经预测控制的研究(Matlab代码实现)

热门文章

  1. PHP每一章的参考题,php 试题总结(附参考答案)
  2. 如何做会说话的班主任
  3. 【BOOST C++ (5)算法】【01】 Boost.Algorithm
  4. java在一台电脑上装两个或多个jdk如何配置环境变量,并实现jdk切换
  5. Java如何对类进行单元测试(unit test)
  6. 多线程-线程之间的通信
  7. 现代控制理论-秩判据和PBH判据
  8. MSP430-GRACE 实战(二):按键中断
  9. UltraEdit批量修改文件内容
  10. Spring Cloud Gateway整合Nacos实现服务路由及集群负载均衡