-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0427-construct-quad-tree.py
More file actions
40 lines (31 loc) · 1.2 KB
/
0427-construct-quad-tree.py
File metadata and controls
40 lines (31 loc) · 1.2 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
# time complexity: O(n^2logn)
# space complexity: O(n^2)
from typing import List
class Node:
def __init__(self, val, isLeaf, topLeft=None, topRight=None, bottomLeft=None, bottomRight=None):
self.val = val
self.isLeaf = isLeaf
self.topLeft = topLeft
self.topRight = topRight
self.bottomLeft = bottomLeft
self.bottomRight = bottomRight
class Solution:
def construct(self, grid: List[List[int]]) -> 'Node':
return self.helper(grid, 0, 0, len(grid))
def helper(self, grid, i, j, w):
if self.allSame(grid, i, j, w):
return Node(grid[i][j] == 1, True)
node = Node(True, False)
node.topLeft = self.helper(grid, i, j, w // 2)
node.topRight = self.helper(grid, i, j + w // 2, w // 2)
node.bottomLeft = self.helper(grid, i + w // 2, j, w // 2)
node.bottomRight = self.helper(grid, i + w // 2, j + w // 2, w // 2)
return node
def allSame(self, grid, i, j, w):
for x in range(i, i + w):
for y in range(j, j + w):
if grid[x][y] != grid[i][j]:
return False
return True
grid = [[0, 1], [1, 0]]
print(Solution().construct(grid))