-
Notifications
You must be signed in to change notification settings - Fork 3
/
29-construct-quad-tree.go
49 lines (45 loc) · 985 Bytes
/
29-construct-quad-tree.go
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
// https://leetcode-cn.com/problems/construct-quad-tree/submissions/
/**
* Definition for a QuadTree node.
* type Node struct {
* Val bool
* IsLeaf bool
* TopLeft *Node
* TopRight *Node
* BottomLeft *Node
* BottomRight *Node
* }
*/
func val(grid [][]int, t, b, l, r int) int {
v := -1
for i := t; i < b; i++ {
for j := l; j < r; j++ {
if v == -1 {
v = grid[i][j]
}
if v != grid[i][j] {
return -1
}
}
}
return v
}
func dfs(grid [][]int, t, b, l, r int) *Node {
v := val(grid, t, b, l, r)
node := Node{}
if v != -1 {
node.IsLeaf = true
if v == 1 {
node.Val = true
}
return &node
}
node.TopLeft = dfs(grid, t, t+(b-t)/2, l, l+(r-l)/2)
node.TopRight = dfs(grid, t, t+(b-t)/2, l+(r-l)/2, r)
node.BottomLeft = dfs(grid, t+(b-t)/2, b, l, l+(r-l)/2)
node.BottomRight = dfs(grid, t+(b-t)/2, b, l+(r-l)/2, r)
return &node
}
func construct(grid [][]int) *Node {
return dfs(grid, 0, len(grid), 0, len(grid))
}