[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 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:
N
is less than1000
and guaranteened to be a power of 2.- If you want to know more about the quad tree, you can refer to its wiki.
"""
# Definition for a QuadTree node.
class Node(object):
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(object):
def construct(self, grid):
"""
:type grid: List[List[int]]
:rtype: Node
"""
if not grid:
return None
if self.isLeaf(grid):
return Node(grid[0][0]==1,True,None,None,None,None)
N=len(grid)
return Node('*',False,self.construct([rows[:N/2] for rows in grid[:N/2]]),self.construct([rows[N/2:] for rows in grid[:N/2]]),self.construct([rows[:N/2] for rows in grid[N/2:]]),self.construct([rows[N/2:] for rows in grid[N/2:]])) def isLeaf(self,grid):
if not grid:
return True
a=set()
for i in grid:
for j in range(len(grid)):
a.add(i[j])
if len(a)>1:
return False
return True
[LeetCode&Python] Problem 427. Construct Quad Tree的更多相关文章
- 【leetcode】427. Construct Quad Tree
problem 427. Construct Quad Tree 参考 1. 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 解题报告
题目要求 We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be tru ...
- [LeetCode&Python] Problem 606. Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- [LeetCode&Python] Problem 226. Invert Binary Tree
Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Tr ...
- [LeetCode&Python] Problem 492. Construct the Rectangle
For a web developer, it is very important to know how to design a web page's size. So, given a speci ...
- [LeetCode] 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&Python] Problem 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
随机推荐
- 如何仅用递归函数和栈操作逆序一个栈——你要先用stack实现,再去改成递归——需要对递归理解很深刻才能写出来
/** * 如何仅用递归函数和栈操作逆序一个栈 * 题目: * 一个栈依次压入1,2,3,4,5,那么从栈顶到栈底分别为5,4,3,2,1. * 将这个栈转置后,从栈顶到栈底为1,2,3,4,5,也就 ...
- weblogic控制台用户名密码修改
1.记得用户名密码但想修改密码修改方法 保存后立即生效,即你退出后即能以新密码登录:但由于启动的用户名密码和登录的用户名密码是同一个,所以我们需要去修改DOMAIN_HOME/servers/serv ...
- vim 文本会在末尾自动添加换行 md5文件和数据只不对应
在linux系统 vim md5data # 打开文件 写入 abc 保存 md5sum md5Data 的计算值和openssl代码计算值不一样 原因在于linux文本文件末尾自动添加了换行 解 ...
- Easyui的datagrid的editor(行编辑器)如何扩展datetimebox类型
在easyui的datagrid扩展方法中添加这样的时间日期(datetimebox)代码块 放在 $.extend($.fn.datagrid.defaults.editors,{datetim ...
- learning at commad AT+CPSI
[Purpose] Learning how to get mobile network info [Eevironment] Shell terminal, base on gcom command ...
- Linux运维工程师真实的工作状态到底是怎么样的?
现在的运维工程师在大家眼中是个什么样子呢? 是不是还是把服务器搬来搬去,每天不是在拿着Linux光盘开始装系统,就是在等待系统安装完成.你如果还是这么想,那就大错特错了.现在又有做一个新的物种诞生,那 ...
- windows下的pycharm配置 linux环境
由于最近学习python的需要,为了方便程序的调试,尝试在Windows下的Pycharm远程连接到虚拟机中Centos下的python环境.(这里我采用的是ssh的远程连接)1.准备工作: 固定ce ...
- QuickHit 项目
package cn.javaoppday01; import java.util.Random; public class Game { public Player player; public G ...
- jdk8-全新时间和日期api
1.jdk8日期和时间api是线程安全的 1.java.time 处理日期时间 2.java.time.temporal: 时间校正器.获取每个月第一天,周几等等 3.java.time.forma ...
- DevExpress v18.1新版亮点——Reporting篇(四)
用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress Reporting v18.1 的新功能,快来下载试用新版本 ...