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.
"""
# 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的更多相关文章

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

  2. 【leetcode】427. Construct Quad Tree

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

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

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

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

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

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

  8. [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 ...

  9. [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 ...

随机推荐

  1. rabbitmq报错type

    TypeError: exchange_declare() got an unexpected keyword argument 'type' 原因应该为pika版本不同导致的用法不同,解决方法为把t ...

  2. C++ char float int string 之间的转换

    string str = "123"; string 转 int int i = atoi( str.c_str() ); string 转 float float f = ato ...

  3. win 7 64 安装 MondgoDB 3.4

    https://jingyan.baidu.com/article/f3e34a12ac10cef5eb653583.html mongod --dbpath "D:\Program Fil ...

  4. Could not connect to '192.168.89.144' (port 22)

    xshell连接linux主机时,会出现错误:Could not connect to '192.168.89.144' (port 22): Connection failed. 但是这时能ping ...

  5. 机器学习与R语言:kNN

    #---------------------------------------- # 功能描述:演示kNN建模过程 # 数据集:威斯康星乳腺癌诊断 # #---------------------- ...

  6. github代码上传下载慢问题

    绑上下面的host,实测下载速度可提高2倍左右. 151.101.72.249 github.global.ssl.fastly.net

  7. 20145313张雪纯 《Java程序设计》第9周学习总结

    20145313张雪纯 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作,开发人员无需接 ...

  8. 20135320赵瀚青LINUX第五章读书笔记

    第五章--系统调用 5.1 与内核通信 作用 1.为用户空间提供一种硬件的抽象接口 2.保证系统稳定和安全 3.除异常和陷入,是内核唯一的合法入口. API.POSIX和C库 关于Unix接口设计:提 ...

  9. 组播的介绍以及CS模型实现

    1.组播介绍 组播组可以是永久的也可以是临时的.组播组地址中,有一部分由官方分配的,称为永久组播组. 永久组播组保持不变的是它的ip地址,组中的成员构成可以发生变化.永久组播组中成员的数量都可以是任意 ...

  10. lucene 简介和实践 分享

    之前项目做了搜索的改造,使用lucene,公司内做了相关的技术分享,故先整理下ppt内容,后面会再把项目中的具体做法进行介绍 lucene 简介和实践  分享 搜索改造项目