给你一个 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;
}
我们可以按以下步骤为二维区域构建四叉树:
如果当前网格的值相同(即,全为 0 或者全为 1),将 isLeaf 设为 True ,将 val 设为网格相应的值,并将四个子节点都设为 Null 然后停止。
如果当前网格的值不同,将 isLeaf 设为 False, 将 val 设为任意值,然后如下图所示,将当前网格划分为四个子网格。
使用适当的子网格递归每个子节点。
/*
// 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[][] g;
public Node construct(int[][] grid) {
g = grid;
return dfs(0,0,grid.length-1,grid.length-1);
}
public Node dfs(int a,int b,int c, int d){
boolean isSame = true;
int curFirst = g[a][b];
//检测矩阵内部元素是否全部一致
for(int i = a;i <=c && isSame;i++ ){
for(int j = b;j <= d && isSame;j++){
if(g[i][j] != curFirst){
isSame = false;
}
}
}
if(isSame){
//等于1说明整个矩阵都是1,设置value为1(true),isLeaf为true
return new Node(curFirst == 1,true);
}
//value设置为任意值
Node root = new Node(curFirst == 1,false);
int dx = c-a+1;
int dy = d-b+1;
root.topLeft = dfs(a,b,a+dx/2-1,b+dy/2-1);
root.topRight = dfs(a,b+dy/2,a+dx/2-1,d);
root.bottomLeft = dfs(a+dx/2,b,c,d-dx/2);
root.bottomRight = dfs(a+dx/2,b+dy/2,c,d);
return root;
}
}
执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:41.8 MB, 在所有 Java 提交中击败了15.89%的用户