-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path0427-construct-quad-tree.js
54 lines (51 loc) · 1.59 KB
/
0427-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
49
50
51
52
53
54
/**
* 427. Construct Quad Tree
* https://leetcode.com/problems/construct-quad-tree/
* Difficulty: Medium
*
* Given a n * n matrix grid of 0's and 1's only. We want to represent grid with a Quad-Tree.
*
* Return the root of the Quad-Tree representing grid.
*
* A Quad-Tree is a tree data structure in which each internal node has exactly four children.
* Besides, each node has two attributes:
* - val: True if the node represents a grid of 1's or False if the node represents a grid of
* 0's. Notice that you can assign the val to True or False when isLeaf is False, and both are
* accepted in the answer.
* - isLeaf: True if the node is a leaf node on the tree or False if the node has four children.
*/
/**
* // 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) {
return build(0, 0, grid.length);
function build(x, y, n) {
const value = grid[x][y];
for (let i = x; i < x + n; i++) {
for (let j = y; j < y + n; j++) {
if (grid[i][j] !== value) {
n /= 2;
return new _Node(true, false,
build(x, y, n),
build(x, y + n, n),
build(x + n, y, n),
build(x + n, y + n, n)
);
}
}
}
return new _Node(value === 1, true, null, null, null, null);
}
};