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 ...
随机推荐
- ModelScope初探:一行代码调用成熟AI模型。
简介: 如何用一行代码调用成熟AI模型?试试ModelScope,让AI开发者解放生产力! ModelScope是阿里推出的下一代开源的模型即服务共享平台,为泛AI开发者提供灵活.易用.低成本的一站式 ...
- 一首歌的时间,手把手搭建基于FC的网站
简介: 不是杰伦的那一首歌啦~ 部署网站 说好不哭 在接触serverless架构之前,我们如果想实现上线一个Web网站,就要在开发前期经过操作很多冗杂但又必须的步骤,不少小白可谓是快速的从入门到退坑 ...
- CDN应用进阶 | 正确使用CDN 让你更好规避安全风险
为了帮助用户更好地了解和使用CDN产品,CDN应用实践进阶系统课程开课了.12月17日,阿里云CDN产品专家彭飞在线分享了<正确使用CDN,让你更好规避安全风险>议题,内容主要包括以下几个 ...
- 基于 KubeVela 与 Kubernetes 打造“无限能力”的开放 PaaS
简介: 本文整理自阿里云容器技术专家.OAM 规范主要制定者之一.KubeVela 作者和负责人孙健波(天元)在阿里云开发者社区"周二开源日"的直播分享,将剖析当前 Kuberne ...
- Serverless 极致弹性解构在线游戏行业痛点
简介: 本文将通过剖析一个个具体的场景案例,以期望给相关的游戏开发同学带来共鸣,同时也希望能给非游戏行业的同学带来一些启发. 一.前言 1. 游戏客户上云关注点 游戏行业是一个富有创意又竞争激烈的市场 ...
- WPF 推荐一个剪贴板内容查看工具
本文来安利大家一个好用的 Windows 剪贴板的内容查看工具 这是在 GitHub 上完全免费开源的应用,由 walterlv 开发的应用,详细请看 https://github.com/walte ...
- 2019-11-29-如何入门-C++-AMP-教程
title author date CreateTime categories 如何入门 C++ AMP 教程 lindexi 2019-11-29 08:20:37 +0800 2018-2-13 ...
- jqGrid--统计列
//数据表格 <div class="gridPanel" style="width:100%;"> @* 数据表格 *@ <table id ...
- Codeforces Round 917 (Div. 2)
A. Least Product 存在 \(a[i] = 0\),\(min = 0\),不需要任何操作. 负数个数为偶数(包括0),\(min = 0\),把任意一个改为 \(0\). 负数个数为奇 ...
- 【VMware vCenter】连接和使用vCenter Server嵌入式vPostgres数据库。
vCenter Server 早期支持内嵌(embedded)和外部(external)数据库,内嵌数据库就是vPostgres,基于VMware Postgres数据库(PostgreSQL数据库) ...