当前位置:网站首页>力扣解法汇总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;
}
}
}边栏推荐
- Is there a female Bluetooth headset suitable for girls? 38 Bluetooth headsets worth getting started
- Huawei, this is too strong
- SQL calculates KS, AUC, IV, psi and other risk control model indicators
- The release of star ring kundb 2.2 provides a new choice for business systems with high concurrent transactions and queries
- Wechat applet - a case of comparing the size of numbers
- Explore performance optimization! Performance improvement from 2 months to 4 hours!
- 力扣解法汇总473-火柴拼正方形
- 力扣解法汇总497-非重叠矩形中的随机点
- LeetCode Algorithm 1791. 找出星型图的中心节点
- Leetcode 55 jump game
猜你喜欢

Operating mechanism of Google ads bidding

竞价广告每次点击出价多少钱是固定的吗?

Tiobe - programming language ranking in June 2022

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

On the night of the joint commissioning, I beat up my colleagues

如何让杀毒软件停止屏蔽某个网页?以GDATA为例

Huawei intermodal game or application review rejected: the application detected payment servicecatalog:x6

Explore performance optimization! Performance improvement from 2 months to 4 hours!

ACL2022 | DCSR:一种面向开放域段落检索的句子感知的对比学习方法

How to stop anti-virus software from blocking a web page? Take gdata as an example
随机推荐
BaseDexClassLoader那些事
Is the bidding price fixed for each click?
Does the virtual host have independent IP
Advantages of Google ads
“中国东信杯”广西大学第四届程序设计竞赛(同步赛)
2022 blind box applet app has become a new drainage outlet for enterprises
Subject knowledge and educational ability of information technology in the first half of 2019 – subjective questions
LeetCode LCP 07. Deliver information
没有文笔,大家多多包涵
android html5页面加载缓存优化
C language programming classic games - minesweeping
通用树形结构的迭代与组合模式实现方案
Explore performance optimization! Performance improvement from 2 months to 4 hours!
力扣解法汇总-剑指 Offer II 114. 外星文字典
kali安装empire过程中遇到的各种报错解决方案
serialization and deserialization
lua 函数
Tiobe - programming language ranking in June 2022
LeetCode Algorithm 1791. 找出星型图的中心节点
Don't miss it! Five large data visualization screens that HR must collect