-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstruct-quad-tree.js
48 lines (42 loc) · 1.36 KB
/
construct-quad-tree.js
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
/**
* // Definition for a QuadTree node.
* function Node(val,isLeaf,topLeft,topRight,bottomLeft,bottomRight) {
* this.val = val;
* this.isLeaf = isLeaf;
* this.topLeft = topLeft;
* this.topRight = topRight;
* this.bottomLeft = bottomLeft;
* this.bottomRight = bottomRight;
* };
*/
/**
* @param {number[][]} grid
* @return {Node}
*/
var construct = function(grid) {
if(grid.length == 1) return new Node(grid[0][0], true, null, null, null, null);
let topLeft = [];
let topRight = [];
let bottomLeft = [];
let bottomRight = [];
for (let i = 0; i < grid.length; i++) {
if(i < grid.length/2) {
topLeft.push(grid[i].slice(0, grid.length/2));
topRight.push(grid[i].slice(grid.length/2));
} else {
bottomLeft.push(grid[i].slice(0, grid.length/2));
bottomRight.push(grid[i].slice(grid.length/2));
}
}
topLeft = construct(topLeft);
topRight = construct(topRight);
bottomLeft = construct(bottomLeft);
bottomRight = construct(bottomRight);
let leaf =
topLeft.isLeaf && topRight.isLeaf && bottomLeft.isLeaf && bottomRight.isLeaf
&& topLeft.val == topRight.val
&& topLeft.val == bottomLeft.val
&& topLeft.val == bottomRight.val;
if(leaf) return new Node(topLeft.val, true, null, null, null, null);
else return new Node(1, false, topLeft, topRight, bottomLeft, bottomRight);
};