题目描述
94、建立四叉树
94、建立四叉树

参照这个博客
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);
    }
}

相关文章:

  • 2021-12-25
  • 2022-01-17
  • 2021-07-29
  • 2021-12-18
  • 2022-12-23
  • 2021-04-19
  • 2021-05-20
猜你喜欢
  • 2021-04-18
  • 2021-04-03
  • 2021-08-27
  • 2022-12-23
  • 2021-09-06
  • 2021-05-24
相关资源
相似解决方案