Skip to content

Commit 5912c0d

Browse files
committed
427/js
1 parent 6c21b92 commit 5912c0d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

427.construct-quad-tree.0.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @lc app=leetcode id=427 lang=javascript
3+
*
4+
* [427] Construct Quad Tree
5+
*/
6+
/**
7+
* // Definition for a QuadTree node.
8+
* function Node(val,isLeaf,topLeft,topRight,bottomLeft,bottomRight) {
9+
* this.val = val;
10+
* this.isLeaf = isLeaf;
11+
* this.topLeft = topLeft;
12+
* this.topRight = topRight;
13+
* this.bottomLeft = bottomLeft;
14+
* this.bottomRight = bottomRight;
15+
* };
16+
*/
17+
/**
18+
* @param {number[][]} grid
19+
* @return {Node}
20+
*/
21+
const construct = function(grid) {
22+
const len = grid.length
23+
24+
if (len == 1) {
25+
return new Node(grid[0][0] == 1 ? true : false, true)
26+
} else {
27+
const g1 = construct(grid.slice(0, len / 2).map(it => it.slice(0, len / 2)))
28+
const g2 = construct(grid.slice(0, len / 2).map(it => it.slice(len / 2)))
29+
const g3 = construct(grid.slice(len / 2).map(it => it.slice(0, len / 2)))
30+
const g4 = construct(grid.slice(len / 2).map(it => it.slice(len / 2)))
31+
32+
const isLeaf = g1.isLeaf && g2.isLeaf && g3.isLeaf && g4.isLeaf
33+
const isSame = g1.val == g2.val && g2.val == g3.val && g3.val == g4.val
34+
35+
return (isLeaf && isSame) ? new Node(g1.val, true) : new Node(false, false, g1, g2, g3, g4)
36+
}
37+
}

0 commit comments

Comments
 (0)