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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值