当前位置:网站首页>力扣解法汇总449-序列化和反序列化二叉搜索树
力扣解法汇总449-序列化和反序列化二叉搜索树
2022-06-12 02:03:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建。
设计一个算法来序列化和反序列化 二叉搜索树 。 对序列化/反序列化算法的工作方式没有限制。 您只需确保二叉搜索树可以序列化为字符串,并且可以将该字符串反序列化为最初的二叉搜索树。
编码的字符串应尽可能紧凑。
示例 1:
输入:root = [2,1,3]
输出:[2,1,3]
示例 2:
输入:root = []
输出:[]
提示:
树中节点数范围是 [0, 104]
0 <= Node.val <= 104
题目数据 保证 输入的树是一棵二叉搜索树。
通过次数27,875提交次数47,327
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/serialize-and-deserialize-bst
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 我们可以按照层序遍历的概念来解题。 * 序列化的时候,我们每次遍历当前层级,都记录下一层级,同时生成下一层级的字符串。如果下一层级不全为空,则加入到最后的字符串当中。 * 反序列化的时候,我们同样设置两个list来记录,分别记录当前和下一层级。以","分割字符串,每次遍历的数量,是上一层级的两倍(不包含为null的情况)。
代码:
public class Solution449 {
public static class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if (root == null) {
return "";
}
List<TreeNode> treeNodes = new ArrayList<>();
List<TreeNode> nextNodes = new ArrayList<>();
treeNodes.add(root);
StringBuilder builder = new StringBuilder();
builder.append(root.val);
builder.append(",");
StringBuilder lineBuilder = new StringBuilder();
while (treeNodes.size() > 0) {
builder.append(lineBuilder);
lineBuilder.setLength(0);
nextNodes.clear();
for (TreeNode node : treeNodes) {
if (node.left != null) {
lineBuilder.append(node.left.val);
lineBuilder.append(",");
nextNodes.add(node.left);
} else {
lineBuilder.append("#,");
}
if (node.right != null) {
lineBuilder.append(node.right.val);
lineBuilder.append(",");
nextNodes.add(node.right);
} else {
lineBuilder.append("#,");
}
}
treeNodes.clear();
treeNodes.addAll(nextNodes);
}
return builder.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data.length() == 0) {
return null;
}
String[] split = data.split(",");
TreeNode root = null;
List<TreeNode> treeNodes = new ArrayList<>();
List<TreeNode> nextNodes = new ArrayList<>();
int base = 1;
int start = 0;
while (start < split.length) {
nextNodes.clear();
for (int i = 0; i < base; i++) {
if (start + i >= split.length) {
break;
}
String value = split[start + i];
if (root == null) {
root = new TreeNode(Integer.parseInt(value));
nextNodes.add(root);
continue;
}
if (value.equals("#")) {
continue;
}
TreeNode currentNode = new TreeNode(Integer.parseInt(value));
TreeNode parent = treeNodes.get(i / 2);
if (i % 2 == 0) {
parent.left = currentNode;
} else {
parent.right = currentNode;
}
nextNodes.add(currentNode);
}
start = start + base;
base = nextNodes.size() * 2;
treeNodes.clear();
treeNodes.addAll(nextNodes);
System.out.print("");
}
return root;
}
}
}边栏推荐
- Explore performance optimization! Performance improvement from 2 months to 4 hours!
- 通过搜索广告附加信息让广告更具相关性
- How to restore the redis cluster and retain the complete cluster data after changing the node IP
- 竞价广告每次点击出价多少钱是固定的吗?
- State owned assets into shares, has Jianye real estate stabilized?
- 力扣解法汇总1037-有效的回旋镖
- PHP development 09 article module deletion and article classification writing
- Comprehensive quality of teaching resources in the second half of 2019 - subjective questions
- Subject knowledge and educational ability of information technology in the first half of 2019 – subjective questions
- Design principle [Demeter's Law]
猜你喜欢

Summary of concrete (ground + wall) + Mountain crack data set (classification and target detection)

C language programming classic games - minesweeping

leetcodeSQL:612. Nearest distance on plane

自适应搜索广告有哪些优势?

MySQL table common operation mind map

Graphic data analysis | business cognition and data exploration

2022最全面的Redis事务控制(带图讲解)

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

Three main factors determining advertising quality

Redis实现消息队列的4种方案
随机推荐
Alicloud OSS file upload system
Graphic data analysis | business cognition and data exploration
ACL 2022 | 预训练语言模型和图文模型的强强联合
Manually tear the linked list (insert, delete, sort) and pointer operation
MySQL高级部分知识点
Ozzanmation action system based on SSE
What are the preparations for setting up Google search advertising series?
Leetcode 55 jump game
LeetCode Algorithm 997. Find the town judge
Ce soir - là, j'ai battu mon collègue...
CVPR2022 | iFS-RCNN:一种增量小样本实例分割器
LeetCode Algorithm 997. 找到小镇的法官
Is there a female Bluetooth headset suitable for girls? 38 Bluetooth headsets worth getting started
Tiobe - programming language ranking in June 2022
PHP builds a high-performance API architecture based on sw-x framework (III)
[learn FPGA programming from scratch -20]: quick start chapter - operation steps 4-2-quick use of Altera quartz II tool (Modelsim co simulation, program download to altera development board)
Spiral matrix (skill)
BaseDexClassLoader那些事
"China Dongxin Cup" the fourth programming competition of Guangxi University (synchronous competition)
[adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology