当前位置:网站首页>力扣解法汇总427-建立四叉树
力扣解法汇总427-建立四叉树
2022-06-12 02:03:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
原题链接:力扣
描述:
给你一个 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 设为任意值,然后如下图所示,将当前网格划分为四个子网格。
使用适当的子网格递归每个子节点。
如果你想了解更多关于四叉树的内容,可以参考 wiki 。
四叉树格式:
输出为使用层序遍历后四叉树的序列化形式,其中 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
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/construct-quad-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 构造一个递归的方法,输入参数为数组和范围。 * 在这个递归方法中,如果值全部相同,则返回一个Node节点的isLeaf=true。 * 否则递归调用四次,分别求左上,右上,左下,右下四个范围的Node值。
代码:
public class Solution427 {
public Node construct(int[][] grid) {
Node construct = construct(grid, 0, grid[0].length, 0, grid.length);
return construct;
}
public Node construct(int[][] grid, int startX, int endX, int startY, int endY) {
//是否值相同
boolean isEqual = true;
int value = -1;
flag:
for (int y = startY; y < endY; y++) {
for (int x = startX; x < endX; x++) {
if (value == -1) {
value = grid[y][x];
continue;
}
if (value == grid[y][x]) {
continue;
}
isEqual = false;
break flag;
}
}
Node node = new Node();
if (isEqual) {
node.val = value == 1;
node.isLeaf = true;
return node;
}
node.val = false;
node.isLeaf = false;
int middleX = startX + (endX - startX) / 2;
int mideleY = startY + (endY - startY) / 2;
node.topLeft = construct(grid, startX, middleX, startY, mideleY);
node.topRight = construct(grid, middleX, endX, startY, mideleY);
node.bottomLeft = construct(grid, startX, middleX, mideleY, endY);
node.bottomRight = construct(grid, middleX, endX, mideleY, endY);
return node;
}
static 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;
}
}
}边栏推荐
- [untitled]
- In 2022, the internal promotion of the "MIHA Tour" golden, silver and silver social recruitment started in April and march! Less overtime, good welfare, 200+ posts for you to choose, come and see!
- Metaverse × How will smart cities develop?
- LeetCode Algorithm 997. Find the town judge
- Ozzanation - système d'action basé sur sse
- Linux (centos7) installer mysql - 5.7
- Bracket generation (backtracking)
- Comprehensive quality of teaching resources in the second half of 2019 - subjective questions
- 力扣编程题-解法汇总
- Basedexclassloader
猜你喜欢

The release of star ring kundb 2.2 provides a new choice for business systems with high concurrent transactions and queries

Basedexclassloader

如何最大化的利用各种匹配方式? ——Google SEM

Introduction to SVM

CVPR2022 | iFS-RCNN:一种增量小样本实例分割器

通用树形结构的迭代与组合模式实现方案

BaseDexClassLoader那些事

Data system provider Jidao technology joins dragon lizard community

How WPS inserts a directory and the operating steps for quickly inserting a directory

Glfwpollevents() program crash
随机推荐
Ozzanmation action system based on SSE
CVPR2022 | iFS-RCNN:一种增量小样本实例分割器
Navicat for MySQL 11 Linux 破解方法
自适应搜索广告有哪些优势?
Four schemes for redis to implement message queue
Metaverse × How will smart cities develop?
力扣解法汇总699-掉落的方块
Almost all schools will ask for the second round exam! Come in and recite the answer!
广泛匹配修饰符符号已经被弃用,请勿使用
Graphic data analysis | data cleaning and pretreatment
Explore performance optimization! Performance improvement from 2 months to 4 hours!
MySQL advanced knowledge points
How to automatically color cells in Excel
力扣解法汇总875-爱吃香蕉的珂珂
Basedexclassloader
Wide match modifier symbol has been deprecated, do not use
BaseDexClassLoader那些事
In 2022, the internal promotion of the "MIHA Tour" golden, silver and silver social recruitment started in April and march! Less overtime, good welfare, 200+ posts for you to choose, come and see!
State owned assets into shares, has Jianye real estate stabilized?
Lua function