【中等】建立四叉树——(分治)

题目描述

给你一个 n * n 矩阵 grid ,矩阵由若干 0 和 1 组成。请你用四叉树表示该矩阵 grid 。

你需要返回能表示矩阵的 四叉树 的根结点。

注意,当 isLeaf 为 False 时,你可以把 True 或者 False 赋值给节点,两种值都会被判题机制 接受 。

四叉树数据结构中,每个内部节点只有四个子节点。此外,每个节点都有两个属性:

  val:储存叶子结点所代表的区域的值。1 对应 True,0 对应 False;
  isLeaf: 当这个节点是一个叶子结点时为 True,如果它有 4 个子节点则为 False 。

class Node {
    public boolean val;
    public boolean isLeaf;
    public Node topLeft;
    public Node topRight;
    public Node bottomLeft;
    public Node bottomRight;
}

我们可以按以下步骤为二维区域构建四叉树:

  1. 如果当前网格的值相同(即,全为 0 或者全为 1),将 isLeaf 设为 True ,将 val 设为网格相应的值,并将四个子节点都设为 Null 然后停止。
  2. 如果当前网格的值不同,将 isLeaf 设为 False, 将 val 设为任意值,然后如下图所示,将当前网格划分为四个子网格。
  3. 使用适当的子网格递归每个子节点。
在这里插入图片描述

四叉树格式

  输出为使用层序遍历后四叉树的序列化形式,其中 null 表示路径终止符,其下面不存在节点。

  它与二叉树的序列化非常相似。唯一的区别是节点以列表形式表示 [isLeaf, val] 。

  如果 isLeaf 或者 val 的值为 True ,则表示它在列表 [isLeaf, val] 中的值为 1 ;如果 isLeaf 或者 val 的值为 False ,则表示值为 0 。

示例1:

在这里插入图片描述

输入:grid = [[0,1],[1,0]]
输出:[[0,1],[1,0],[1,1],[1,1],[1,0]]
解释:此示例的解释如下:
请注意,在下面四叉树的图示中,0 表示 false,1 表示 True 。
在这里插入图片描述
示例2:
在这里插入图片描述
输入:grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]]
输出:[[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]
解释:网格中的所有值都不相同。我们将网格划分为四个子网格。
topLeft,bottomLeft 和 bottomRight 均具有相同的值。
topRight 具有不同的值,因此我们将其再分为 4 个子网格,这样每个子网格都具有相同的值。
解释如下图所示:

在这里插入图片描述
示例 3:
输入:grid = [[1,1],[1,1]]
输出:[[1,1]]

示例 4:
输入:grid = [[0]]
输出:[[1,0]]

示例 5:
输入:grid = [[1,1,0,0],[1,1,0,0],[0,0,1,1],[0,0,1,1]]
输出:[[0,1],[1,1],[1,0],[1,0],[1,1]]

提示:
n == grid.length == grid[i].length
n == 2^x 其中 0 <= x <= 6

解题思路

明显的分治策略,将每一个输入矩阵切分为上左、上右、下左、下右,递归终止条件为输入矩阵全0或全1。
其递归方程为:
在这里插入图片描述
时间复杂度分析:
T ( n ) = { O ( 1 ) 4 T ( n / 2 ) + O ( 1 ) T(n) = \left\{ \begin{gathered} O(1) \\ 4T(n/2) + O(1) \\ \end{gathered} \right. T(n)={O(1)4T(n/2)+O(1)
时间复杂度为
T ( n ) = O ( 4 k ) , k = log ⁡ 2 n T(n) = O({4^k}),k = {\log _2}^n T(n)=O(4k),k=log2n

代码:

// 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() {
        this.val = false;
        this.isLeaf = false;
        this.topLeft = null;
        this.topRight = null;
        this.bottomLeft = null;
        this.bottomRight = null;
    }
    
    public Node(boolean val, boolean isLeaf) {
        this.val = val;
        this.isLeaf = isLeaf;
        this.topLeft = null;
        this.topRight = null;
        this.bottomLeft = null;
        this.bottomRight = null;
    }
    
    public Node(boolean val, boolean isLeaf, Node topLeft, Node topRight, Node bottomLeft, Node bottomRight) {
        this.val = val;
        this.isLeaf = isLeaf;
        this.topLeft = topLeft;
        this.topRight = topRight;
        this.bottomLeft = bottomLeft;
        this.bottomRight = bottomRight;
    }
};

class Solution {
    int[][] grid;
    public Node construct(int[][] grid) {
        this.grid=grid;
        return construct0(0,0,grid.length);
    }
    public Node construct0(int rowbegin,int colbegin,int n) {
        int scan=isScan(rowbegin,colbegin,n);
        if(scan!=-1) return new Node(scan==1,true);
        int k=n/2;
        Node res=new Node(true,false);
        res.topLeft=construct0(rowbegin,colbegin,k);
        res.topRight=construct0(rowbegin,colbegin+k,k);
        res.bottomLeft=construct0(rowbegin+k,colbegin,k);
        res.bottomRight=construct0(rowbegin+k,colbegin+k,k);
        return res;
    }
    public int isScan(int rowbegin,int colbegin,int n){
        int s=grid[rowbegin][colbegin];
        for (int i=rowbegin;i<rowbegin+n;i++)
            for (int j = colbegin; j < colbegin+n; j++)
                if (s != grid[i][j]) return -1;
        return s;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冷冰殇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值