题目描述
参照这个博客
https://www.cnblogs.com/grandyang/p/9649348.html
Java代码开发
/*
// Definition for a QuadTree node.
class Node {
public boolean val;
public boolean isLeaf;
public Node topLeft;
public Node topRight;
public Node bottomLeft;
public Node bottomRight;
public Node() {}
public Node(boolean _val,boolean _isLeaf,Node _topLeft,Node _topRight,Node _bottomLeft,Node _bottomRight) {
val = _val;
isLeaf = _isLeaf;
topLeft = _topLeft;
topRight = _topRight;
bottomLeft = _bottomLeft;
bottomRight = _bottomRight;
}
};
*/
class Solution {
public Node construct(int[][] grid) {
return build(grid, 0, 0, grid.length);
}
public static Node build(int[][]gird,int x,int y,int len){
if(len <=0 )
return null;
for (int i = x; i < x+len; i++) {
for (int j = y; j < y+len; j++) {
if(gird[i][j]!=gird[x][y]){
return new Node(true,false,build(gird, x, y, len / 2),
build(gird, x, y + len / 2, len / 2),
build(gird, x + len/ 2, y, len / 2),
build(gird, x + len / 2, y + len / 2, len / 2));
}
}
}
return new Node(gird[x][y] == 1, true, null, null, null, null);
}
}
还需要弄,没懂!!!!!!!!!!!!
排名靠前的代码
class Solution {
public Node construct(int[][] grid) {
return construct(grid,0,0,grid.length);
}
public Node construct(int[][] grid, int r, int c, int ss) {
int target = grid[r][c];
for(int i=r; i<r+ss; i++) {
for(int j=c; j<c+ss; j++) {
if(target != grid[i][j]) {
return new Node(true, false,
construct(grid,r, c , ss/2),
construct(grid,r, c+ss/2, ss/2),
construct(grid,r+ss/2,c , ss/2),
construct(grid,r+ss/2,c+ss/2, ss/2));
}
}
}
return new Node((target == 1) ? true : false, true, null, null, null, null);
}
}