LeetCode 449. Serialize and Deserialize BST 序列化和反序列化二叉搜索树 (Java)
题目:
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure.
The encoded string should be as compact as possible.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
分析:
序列化一颗二叉搜索树,可以和序列化反序列化二叉树用相同的做法,这次就用层次遍历来做。
程序:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec { // Encodes a tree to a single string.
public String serialize(TreeNode root) {
if(root == null)
return "";
StringBuilder str = new StringBuilder();
LinkedList<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int len = queue.size();
LinkedList<TreeNode> temp = new LinkedList<>();
for(int i = 0; i < len; ++i){
TreeNode node = queue.removeFirst();
if (node != null) {
temp.add(node.left);
temp.add(node.right);
str.append(node.val).append(",");
} else {
str.append("#,");
}
}
queue = temp;
//System.out.println(queue.toString());
}
return str.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data == "" || data == null)
return null;
String[] strs = data.split(",");
Queue<String> l = new ArrayDeque<>(Arrays.asList(strs));
Queue<TreeNode> nodes = new ArrayDeque<>();
TreeNode root = new TreeNode(Integer.parseInt(l.poll()));
nodes.offer(root);
while(!nodes.isEmpty() && !l.isEmpty()){
TreeNode node = nodes.poll();
String strl = l.poll();
if(!strl.equals("#")){
node.left = new TreeNode(Integer.parseInt(strl));
nodes.offer(node.left);
}
String strr = l.poll();
if(!strr.equals("#")){
node.right = new TreeNode(Integer.parseInt(strr));
nodes.offer(node.right);
}
}
return root;
} } // Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
LeetCode 449. Serialize and Deserialize BST 序列化和反序列化二叉搜索树 (Java)的更多相关文章
- 449 Serialize and Deserialize BST 序列化和反序列化二叉搜索树
详见:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ C++: /** * Definition fo ...
- [leetcode]449. Serialize and Deserialize BST序列化反序列化二叉搜索树(尽量紧凑)
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [leetcode]449. Serialize and Deserialize BST序列化与反序列化BST
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- Java实现 LeetCode 449 序列化和反序列化二叉搜索树
449. 序列化和反序列化二叉搜索树 序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建. 设计一个算法 ...
- 【leetcode-449】序列化和反序列化二叉搜索树
序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建. 设计一个算法来序列化和反序列化二叉搜索树. 对序列 ...
- LeetCode 449. Serialize and Deserialize BST
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...
- [leetcode]449. Serialize and Deserialize BST设计BST的编解码
这道题学到了东西. /* 一开始想着中序遍历,但是解码的时候才发现,中序遍历并不能唯一得确定二叉树. 后来看了网上的答案,发现先序遍历是可以的,观察了一下,对于BST,先序遍历确实是可以 唯一得确定. ...
- 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)
[LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...
- 【leetcode】449. Serialize and Deserialize BST
题目如下: Serialization is the process of converting a data structure or object into a sequence of bits ...
- 449. Serialize and Deserialize BST
https://leetcode.com/problems/serialize-and-deserialize-bst/#/description Serialization is the proce ...
随机推荐
- 【pytorch学习】之数据预处理
2 数据预处理 为了能用深度学习来解决现实世界的问题,我们经常从预处理原始数据开始,而不是从那些准备好的张量格式数据开始.在Python中常用的数据分析工具中,我们通常使用pandas软件包.像庞大的 ...
- HarmonyOS NEXT应用开发—自定义视图实现Tab效果
介绍 本示例介绍使用Text.List等组件,添加点击事件onclick,动画,animationTo实现自定义Tab效果. 效果预览图 使用说明 点击页签进行切换,选中态页签字体放大加粗,颜色由灰变 ...
- 急速上线 Serverless 钉钉机器人“防疫精灵”
新型冠状病毒疫情肆虐的春节,大家都过得人心惶惶,作为被关在家的程序狗,总觉得要做点什么.于是阿里云 IoT 事业部的几个同学就开始了防疫精灵的开发之路. 从点子到防疫宝,只花了一个下午时间:从防疫宝到 ...
- 使用云效Codeup10分钟紧急修复Apache Log4j2漏洞
简介:2021年12月10日,国家信息安全漏洞共享平台(CNVD)收录了Apache Log4j2远程代码执行漏洞(CNVD-2021-95914),此漏洞是一个基于Java的日志记录工具,为Log ...
- 21.3K star!推荐一款可视化自动化测试/爬虫/数据采集神器!功能免费且强大!
大家好,我是狂师! 在大数据时代,信息的获取与分析变得尤为重要.对于开发者.数据分析师乃至非技术人员来说,能够高效地采集网络数据并进行分析是一个强有力的工具.今天,我要向大家推荐的是一款功能强大.操作 ...
- dotnet DirectX 通过 Vortice 控制台使用 ID2D1DeviceContext 绘制画面
在上一篇博客里面告诉大家,如何使用 Vortice 从零开始控制台创建 Direct2D1 窗口.上一篇博客采用的是 CreateDxgiSurfaceRenderTarget 的方式拿到了 ID2D ...
- 2018-11-19-WPF-使用-SharpDX-在-D3DImage-显示
title author date CreateTime categories WPF 使用 SharpDX 在 D3DImage 显示 lindexi 2018-11-19 15:38:35 +08 ...
- k8s核心资源Job&&CronJob调度pod
一.Job与CronJob概念与与原理解读 Job 控制器用于管理 Pod 对象运行一次性任务,比方说我们对数据库备份,可以直接在 k8s 上启动一个 mysqldump 备份程序,也可以启动一个 p ...
- SQL 多表关联更新语句
A表WHERE条件来自B表查询结果集 UPDATE a SET a.is_sync = 0 FROM A表 a LEFT JOIN B表 b ON a.order_id = b.order_id AN ...
- Solution Set - CDQ分治&整体二分
A[洛谷P2163].给定平面上若干个点,多次询问给定矩形内的点数. B[洛谷P3810].给定若干个三元组,对所有\(k\),求这样三元组的个数:恰有\(k\)个三元组,满足其每个分量都不超过它的相 ...