-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0427-construct-quad-tree.kt
44 lines (37 loc) · 1.22 KB
/
0427-construct-quad-tree.kt
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
/**
* Definition for a QuadTree node.
* class Node(var `val`: Boolean, var isLeaf: Boolean) {
* var topLeft: Node? = null
* var topRight: Node? = null
* var bottomLeft: Node? = null
* var bottomRight: Node? = null
* }
*/
class Solution {
fun construct(grid: Array<IntArray>): Node? {
fun dfs(n: Int, r: Int, c: Int): Node? {
var allSame = true
for(i in 0 until n) {
for(j in 0 until n) {
if(grid[r][c] != grid[r + i][c + j]) {
allSame = false
break
}
}
}
if(allSame) return Node(if(grid[r][c] == 1) true else false, true)
val nextN = n/2
val topLeft = dfs(nextN, r, c)
val topRight = dfs(nextN, r, c + nextN)
val bottomLeft = dfs(nextN, r + nextN, c)
val bottomRight = dfs(nextN, r + nextN, c + nextN)
val node = Node(false, false)
node.topLeft = topLeft
node.topRight = topRight
node.bottomLeft = bottomLeft
node.bottomRight = bottomRight
return node
}
return dfs(grid.size, 0, 0)
}
}