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.
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
"""
def construct2(g, i1, j1, n):
s = g[i1][j1]
is_same = True
for i in xrange(i1, i1+n):
for j in xrange(j1, j1+n):
if g[i][j] != s:
is_same = False
break
if is_same:
return Node(s, True, None, None, None, None)
else:
root = Node("*", False, None, None, None, None)
root.topLeft = construct2(g, i1, j1, n/2)
root.topRight = construct2(g, i1, j1+n/2, n/2)
root.bottomLeft = construct2(g, i1+n/2, j1, n/2)
root.bottomRight = construct2(g, i1+n/2, j1+n/2, n/2)
return root return construct2(grid, 0, 0, len(grid))
其他解法,直接是dfs:
class Solution:
def construct(self, grid):
def dfs(x, y, l):
if l == 1:
node = Node(grid[x][y] == 1, True, None, None, None, None)
else:
tLeft = dfs(x, y, l // 2)
tRight = dfs(x, y + l // 2, l // 2)
bLeft = dfs(x + l // 2, y, l// 2)
bRight = dfs(x + l // 2, y + l // 2, l // 2)
value = tLeft.val or tRight.val or bLeft.val or bRight.val
if tLeft.isLeaf and tRight.isLeaf and bLeft.isLeaf and bRight.isLeaf and tLeft.val == tRight.val == bLeft.val == bRight.val:
node = Node(value, True, None, None, None, None)
else:
node = Node(value, False, tLeft, tRight, bLeft, bRight)
return node
return grid and dfs(0, 0, len(grid)) or None
leetcode 427. Construct Quad Tree的更多相关文章
- 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】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&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 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- (二叉树 递归) 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 that ...
- [LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [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 that ...
- [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 ...
随机推荐
- 42. Trapping Rain Water(直方图 存水量 hard)
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- java.lang.OutOfMemoryError: PermGen space异常及解决
PermGen space的全称是Permanent Generation space,是指内存的永久保存区域,这块内存主要是被JVM存放Class和Meta信息的,Class在被Loader时就会被 ...
- Web前端开发学习笔记(一)
最近在复习Web前端的开发知识,于是就把大二上学期曾经学过的东西拿出来复习一遍,把自己在做曾经的作业时遇到有意义的点都记下来吧. Homework1:http://my.ss.sysu.edu.cn/ ...
- Python3.x:代理ip刷点赞
Python3.x:代理ip刷点赞 声明:仅供为学习材料,不允许用作商业用途: 一,功能: 针对某网站对企业自动刷点赞: 网站:https://best.zhaopin.com/ 二,步骤: 1,获取 ...
- hadoop HA+kerberos HA集群搭建
IP.主机名规划 hadoop集群规划: hostname IP hadoop 备注 hadoop1 110.185.225.158 NameNode,ResourceManager,DFSZKFai ...
- Centos 7 关闭邮件服务及禁用IPv6
关闭邮件服务(禁用25端口) sudo systemctl stop dovecot sudo systemctl stop postfix sudo systemctl disable doveco ...
- 防止xss(脚本攻击)的方法之过滤器
一 什么是脚本注入 概念我就不说了 直接百度一份 XSS是一种经常出现在web应用中的计算机安全漏洞,它允许恶意web用户将代码植入到提供给其它用户使用的页面中.比如这些代码包括HTML代码和客户端 ...
- 爬虫之Fiddler抓取HTTPS设置
Fiddler抓取HTTPS设置 启动Fiddler,打开菜单栏中的 Tools > Telerik Fiddler Options,打开“Fiddler Options”对话框. 对Fiddl ...
- Spring MVC web.xml+servlet.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " ...
- Windows下安装mayavi2
由于要使用mayavi2画三维图,但是没有找到二进制包,所以只能安装pythonxy或者canopy之类的版本,后来在http://www.lfd.uci.edu/~gohlke/pythonlibs ...