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 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.
题目分析及思路
需要使用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 解题报告的更多相关文章
- 【LeetCode】427. Construct Quad Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 【leetcode】427. Construct Quad Tree
problem 427. Construct Quad Tree 参考 1. Leetcode_427. Construct Quad Tree; 完
- [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 ...
- 【LeetCode】100. Same Tree 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- LeetCode: Recover Binary Search Tree 解题报告
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- LeetCode 226 Invert Binary Tree 解题报告
题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...
- 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 ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- Node入门教程(11)第九章:Node 的网络模块
net网络模块 net模块是node对TCP或者IPC开发的封装,包括了客户端和服务器端相关API.对于阅读本文,请您有一定的网络编程的基础.您需要已经了解了: ip协议,会配置ip地址 了解dns解 ...
- asp.net Request、Request.Form、Request.QueryString的区别(转)
Request.Form:获取以POST方式提交的数据. Request.QueryString:获取地址栏参数(以GET方式提交的数据). Request:包含以上两种方式(优先获取GET方式提交的 ...
- FTP实验
一.安装 sudo apt-get install vsftpd service vsftpd start 启动vsftpd服务 如果在不设置任何的情况下,可以以匿名的方式访问该ftp. 这时候你可以 ...
- C++ 智能指针二
/* 智能指针shared_ptr注意点 */ #include <iostream> #include <string> #include <memory> // ...
- Python3运算符
一.算术运算符 二.比较运算符 三 .赋值运算符 四 .位运算符 按位运算符是把数字看作二进制来进行计算的.Python中的按位运算法则如下: 下表中变量 a 为 60,b 为 13二进制格式如下: ...
- python3 zip压缩文件压缩多个不同文件夹内的文件方法
#!/usr/bin/env python # -*- coding:utf-8 -*- import zipfile def addzip(): f = zipfile.ZipFile('test. ...
- java 全组合 与全排列
一.全组合 public static void Combination( ) { /*基本思路:求全组合,则假设原有元素n个,则最终组合结果是2^n个.原因是: * 用位操作方法:假设元素原本有:a ...
- laravel 5.4 中使用migrate
1. 创建表结构 a. 命令: php artisan make:migration create_posts_table 2.生产文件 <?php use Illuminate\Support ...
- .NET中进行Base64加密解密
方法一: /// <summary> /// Base64加密 /// </summary> /// <param name="Message"> ...
- 51 IP核查询
康芯的IP核 Oregano systems 公司的MC8051 IP CoreSynthesizeable VHDL Microcontroller IP-Core User Guide这个里面51 ...