leetcode 每日一题 427. 建立四叉树
leetcode 每日一题 427. 建立四叉树
class Solution {
public Node construct(int[][] grid) {
return f(grid, 0, 0, grid.length);
}
private Node f(int[][] grid, int x, int y, int len) {
boolean isLeaf = true;
stop:
for (int i = x; i < x + len; i++) {
for (int j = y; j < y + len; j++) {
if (grid[i][j] != grid[x][y]) {
isLeaf = false;
break stop;
}
}
}
//如果是
if (isLeaf) {
return new Node(grid[x][y] == 1, isLeaf);
}
Node topLeft = f(grid, x, y, len / 2);
Node topRight = f(grid, x, y + len / 2, len / 2);
Node bottomLeft = f(grid, x + len / 2, y, len / 2);
Node bottomRight = f(grid, x + len / 2, y + len / 2, len / 2);
return new Node(true, isLeaf, topLeft,topRight,bottomLeft, bottomRight);
}
}


浙公网安备 33010602011771号