题目要求

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 valisLeaf 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.

题目分析及思路

需要使用quad trees来存储 N x N的布尔值网格,每个网格只能是true或false。根结点表示整个网格。对于每一个结点,要求将自身平均分成四分,直到每一份的值都相等为止。每一个结点还有另外两个布尔属性isLeaf和val。isLeaf是True当且仅当这个结点是叶结点。val则是表示当前叶结点的值,若不是叶结点,则用“*”表示。可以先写一个函数来判断这个区域的值是否相同,之后用递归的方法求解,条件是该结点是叶结点。

python代码

"""

# Definition for a QuadTree node.

class Node:

def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):

self.val = val

self.isLeaf = isLeaf

self.topLeft = topLeft

self.topRight = topRight

self.bottomLeft = bottomLeft

self.bottomRight = bottomRight

"""

class Solution:

def construct(self, grid: List[List[int]]) -> 'Node':

def isthesame(grid):

e = set()

for r in range(len(grid)):

for c in range(len(grid)):

e.add(grid[r][c])

if len(e) == 1 and 1 in e:

return True

elif len(e) == 1 and 0 in e:

return False

else:

return '*'

if isthesame(grid) == True:

return Node(True, True, None, None, None, None)

elif isthesame(grid) == False:

return Node(False, True, None, None, None, None)

else:

l = len(grid)

mid = l // 2

topleft = [[grid[i][j] for j in range(mid)] for i in range(mid)]

topright = [[grid[i][j] for j in range(mid, l)] for i in range(mid)]

bottomleft = [[grid[i][j] for j in range(mid)] for i in range(mid, l)]

bottomright = [[grid[i][j] for j in range(mid, l)] for i in range(mid, l)]

return Node('*', False, self.construct(topleft), self.construct(topright), self.construct(bottomleft), self.construct(bottomright))

LeetCode 427 Construct Quad Tree 解题报告的更多相关文章

  1. 【LeetCode】427. Construct Quad Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 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 true or ...

  3. 【leetcode】427. Construct Quad Tree

    problem 427. Construct Quad Tree 参考 1. Leetcode_427. Construct Quad Tree; 完

  4. [LeetCode&Python] Problem 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 true or ...

  5. 【LeetCode】100. Same Tree 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...

  6. LeetCode: Recover Binary Search Tree 解题报告

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  7. LeetCode 226 Invert Binary Tree 解题报告

    题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...

  8. LeetCode 965 Univalued Binary Tree 解题报告

    题目要求 A binary tree is univalued if every node in the tree has the same value. Return true if and onl ...

  9. LeetCode: Validate Binary Search Tree 解题报告

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

随机推荐

  1. JAVA(四)类集/枚举

    成鹏致远 | lcw.cnblog.com |2014-02-04 JAVA类集 1.认识类集 类集的作用 类集实际上就是一个动态的对象数组,与一般的对象数组不同,类集中的对象内容可以任意扩充 类集的 ...

  2. windows_硬盘上设置虚拟内存

    1)在桌面上的“计算机”或“我的电脑”上右键->属性->高级->性能->设置->高级->虚拟内存->更改. 2)在虚拟内存更改页面,先选择在哪个磁盘上设置虚拟 ...

  3. django 拷贝一个 model 实例

    今天做一个拷贝功能,把某个 obj 拷贝并修改部分数据,提交表单后保存为一个新实例.结果google 出来的结果不对,都是相互copy 的代码,大概如下: obj = MyModel.objects. ...

  4. 利用堆实现堆排序&优先队列

    数据结构之(二叉)堆一文在末尾提到"利用堆能够实现:堆排序.优先队列.".本文代码实现之. 1.堆排序 如果要实现非递减排序.则须要用要大顶堆. 此处设计到三个大顶堆的操作:(1) ...

  5. 【九天教您南方cass 9.1】 12 道路断面土方计算

    同学们大家好,欢迎收看由老王测量上班记出品的cass9.1视频课程 我是本节课主讲老师九天. 我们讲课的教程附件也是共享的,请注意索取 在测量空间中. [点击索取cass教程]5元立得 (给客服说暗号 ...

  6. (转)Windows系统白名单以及UAC机制

    用户帐户控制 深入了解 Windows 7 用户帐户控制 Mark Russinovich   概览: 标准用户帐户 用户帐户控制 内容 UAC 技术 提升与恶意软件安全性 Windows 7 中的不 ...

  7. 块级格式化上下文( Block formatting contexts)

    那么如何触发BFC呢? float 除了none以外的值 overflow 除了visible 以外的值(hidden,auto,scroll ) display (table-cell,table- ...

  8. java 注解默认值

    package com.zejian.annotationdemo; import java.lang.annotation.ElementType; import java.lang.annotat ...

  9. Java知多少(65)线程的挂起、恢复和终止

    有时,线程的挂起是很有用的.例如,一个独立的线程可以用来显示当日的时间.如果用户不希望用时钟,线程被挂起.在任何情形下,挂起线程是很简单的,一旦挂起,重新启动线程也是一件简单的事. 挂起,终止和恢复线 ...

  10. 解决com.mysql.jdbc.PacketTooBigException: Packet for query is too large

    在做查询数据库操作时,报了以上错误,还有out of memery heap hacp ,原因是MySQL的max_allowed_packet设置过小引起的,我一开始设置的是1M,后来改为了20M ...