文章目录

题目

给你一个 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. 使用适当的子网格递归每个子节点。

LeetCode 0427「建立四叉树」_数据结构

四叉树格式:

  • 输出为使用层序遍历后四叉树的序列化形式,其中 null 表示路径终止符,其下面不存在节点。
  • 它与二叉树的序列化非常相似。唯一的区别是节点以列表形式表示 [isLeaf, val] 。
  • 如果 isLeaf 或者 val 的值为 True ,则表示它在列表 [isLeaf, val] 中的值为 1 ;如果 isLeaf 或者 val 的值为 False ,则表示值为 0 。

示例1:

LeetCode 0427「建立四叉树」_四叉树_02

  • 输入:grid = [[0,1],[1,0]]
  • 输出:[[0,1],[1,0],[1,1],[1,1],[1,0]]
  • 解释:此示例的解释如下:请注意,在下面四叉树的图示中,0 表示 false,1 表示 True 。

LeetCode 0427「建立四叉树」_四叉树_03

示例2:

LeetCode 0427「建立四叉树」_算法_04

  • 输入: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 个子网格,这样每个子网格都具有相同的值。

LeetCode 0427「建立四叉树」_leetcode_05

提示:

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

题目来源:LeetCode

分析

此题可以采用递归的方式。定义一个递归函数,传入一个区域范围的网格,函数返回此区域范围网格的根节点。

一开始传入递归函数的是整个原始的网格,在递归函数中,如果判断要此范围的网格的所有单元格值都相同,那么返回叶子节点,递归结束。否则,将此范围网格划分四个小区域网格,然后构建一个带有4个子节点的节点对象返回,子节点依旧调用递归函数计算而来。

实现

package com.chenpi.no0427Construct;

/**
* @author 陈皮
* @version 1.0
* @description
* @date 2022/4/29
*/
public class No0427Construct {

public static void main(String[] args) {
No0427Construct inst = new No0427Construct();
int[][] grid = {{0, 1}, {1, 0}};
Node rootNode = inst.construct(grid);
System.out.println(rootNode);
}

public Node construct(int[][] grid) {
// 返回整个大网格的根节点
return dfs(grid, 0, grid.length, 0, grid.length);
}


public Node dfs(int[][] grid, int beginRow, int endRow, int beginCol, int endCol) {

// 判断此区域的网格的所有节点值是否相同
boolean same = true;
for (int i = beginRow; i < endRow; i++) {
for (int j = beginCol; j < endCol; j++) {
// 直接拿此区域的第一个节点与其他节点比较即可
if (grid[beginRow][beginCol] != grid[i][j]) {
same = false;
break;
}
}
if (!same) {
break;
}
}

// 如果所有节点相同,则是叶子节点
if (same) {
// 叶子节点的值和此区域网格的值相同
return new Node(grid[beginRow][beginCol] == 1, true);
}

// 不是叶子节点,则构建一个带有4个子节点的节点
return new Node(
true,
false,
dfs(grid, beginRow, (beginRow + endRow) / 2, beginCol, (beginCol + endCol) / 2),
dfs(grid, beginRow, (beginRow + endRow) / 2, (beginCol + endCol) / 2, endCol),
dfs(grid, (beginRow + endRow) / 2, endRow, beginCol, (beginCol + endCol) / 2),
dfs(grid, (beginRow + endRow) / 2, endRow, (beginCol + endCol) / 2, endCol)
);
}

}


/**
* 四叉树节点
*/
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;
}

@Override
public String toString() {
return "[" + (isLeaf ? 1 : 0) + "," + (val ? 1 : 0) + "]" + topLeft + topRight + bottomLeft
+ bottomRight;
}
}

Leetcode 执行结果:

LeetCode 0427「建立四叉树」_子节点_06


本次分享到此结束啦~~

如果觉得文章对你有帮助,点赞、收藏、关注、评论,您的支持就是我创作最大的动力!