# 427. Construct Quad Tree 建立四叉树

@TOC

## # 题目描述

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.

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.

## # 解题方法

• 如果一个正方形中所有的数字都是 1，则 `val` 是 True；
• 如果一个正方形中所有的数字都是 0，则 `val` 是 False。
• 否则，说明格子里的值不都是相同的，返回 None.

``````"""
# 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):
"""
:type grid: List[List[int]]
:rtype: Node
"""
_len = len(grid)
if isLeaf == None:
mid = _len // 2
topLeftGrid = [[grid[i][j] for j in range(mid)] for i in range(mid)]
topRightGrid = [[grid[i][j] for j in range(mid, _len)] for i in range(mid)]
bottomLeftGrid = [[grid[i][j] for j in range(mid)] for i in range(mid, _len)]
bottomRightGrid = [[grid[i][j] for j in range(mid, _len)] for i in range(mid, _len)]
node = Node(True, False, self.construct(topLeftGrid), self.construct(topRightGrid),
self.construct(bottomLeftGrid), self.construct(bottomRightGrid))
elif isLeaf == False:
node = Node(False, True, None, None, None, None)
else:
node = Node(True, True, None, None, None, None)
return node

_len = len(grid)
_sum = 0
for i in range(_len):
_sum += sum(grid[i])
if _sum == _len ** 2:
return True
elif _sum == 0:
return False
else:
return None
``````

``````"""
# 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
"""
N = len(grid)
if N == 1:
return Node(grid[0][0] == 1, True, None, None, None, None)
topLeftSum = sum([grid[i][j] for i in range(N/2) for j in range(N/2)])
topRightSum = sum([grid[i][j] for i in range(N/2) for j in range(N/2, N)])
bottomLeftSum = sum([grid[i][j] for i in range(N/2, N) for j in range(N/2)])
bottomRightSum = sum(grid[i][j] for i in range(N/2, N) for j in range(N/2, N))
node = Node(False, False, None, None, None, None)
if topLeftSum == topRightSum == bottomLeftSum == bottomRightSum:
if topLeftSum == 0:
node.isLeaf = True
node.val = False
elif topLeftSum == (N / 2) ** 2:
node.isLeaf = True
node.val = True
if node.isLeaf:
return node
node.val = True
node.topLeft = self.construct([[grid[i][j] for j in range(N/2)] for i in range(N/2)])
node.topRight = self.construct([[grid[i][j] for j in range(N/2, N)] for i in range(N/2)])
node.bottomLeft = self.construct([[grid[i][j] for j in range(N/2)] for i in range(N/2, N)])
node.bottomRight = self.construct([[grid[i][j] for j in range(N/2, N)] for i in range(N/2, N)])
return node
``````

## # 日期

2018 年 8 月 19 日 —— 天阴阴，地潮潮，这种天气真舒服 2018 年 11 月 13 日 —— 时间有点快