-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path427_construct_quad_tree.py
More file actions
63 lines (51 loc) · 2.13 KB
/
Copy path427_construct_quad_tree.py
File metadata and controls
63 lines (51 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
# 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
1 1 0 0
0 0 1 1
1 1 0 0
0 0 1 1
"""
class Solution:
def construct(self, grid: List[List[int]]) -> 'Node':
root = Node(0, 0, None, None, None, None)
n = len(grid)
if n == 1:
root.isLeaf = 1
root.val = grid[0][0]
return root
elif n == 2:
if grid[0][0] == grid[1][1] == grid[0][1] == grid[1][0]:
root.val = grid[0][0]
root.isLeaf = 1
else:
root.val = 1
root.isLeaf = 0
root.topLeft = Node(grid[0][0], 1, None, None, None, None)
root.topRight = Node(grid[0][1], 1, None, None, None, None)
root.bottomLeft = Node(grid[1][0], 1, None, None, None, None)
root.bottomRight = Node(grid[1][1], 1, None, None, None, None)
return root
elif n > 2:
root.topLeft = self.construct([i[:n//2] for i in grid[:n//2]])
root.topRight = self.construct([i[n//2:] for i in grid[:n//2]])
root.bottomLeft = self.construct([i[:n//2] for i in grid[n//2:]])
root.bottomRight = self.construct([i[n//2:] for i in grid[n//2:]])
if root.topLeft.val == root.topRight.val == root.bottomLeft.val == root.bottomRight.val and root.topLeft.isLeaf == root.topRight.isLeaf == root.bottomLeft.isLeaf == root.bottomRight.isLeaf == 1:
root.isLeaf = 1
root.val = root.topLeft.val
root.topLeft = None
root.topRight = None
root.bottomLeft = None
root.bottomRight = None
else:
root.isLeaf = 0
root.val = 1
return root