LeetCode 449. Serialize and Deserialize BST
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/
题目:
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.
题解:
与Serialize and Deserialize Binary Tree相同解法.
Time Complexity: serialize, O(n). deserialize, O(n). Space: O(n).
AC Java:
/**
* 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) {
StringBuilder sb = new StringBuilder();
preorder(root, sb);
return sb.toString();
} private void preorder(TreeNode root, StringBuilder sb){
if(root == null){
sb.append("#").append(",");
return;
}
sb.append(root.val).append(",");
preorder(root.left, sb);
preorder(root.right, sb);
} // Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
LinkedList<String> que = new LinkedList<String>();
que.addAll(Arrays.asList(data.split(",")));
return deserial(que);
} private TreeNode deserial(LinkedList<String> que){
String str = que.pollFirst();
if(str.equals("#")){
return null;
}
TreeNode root = new TreeNode(Integer.valueOf(str));
root.left = deserial(que);
root.right = deserial(que);
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的更多相关文章
- [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 ...
- [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的编解码
这道题学到了东西. /* 一开始想着中序遍历,但是解码的时候才发现,中序遍历并不能唯一得确定二叉树. 后来看了网上的答案,发现先序遍历是可以的,观察了一下,对于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 ...
- 449. Serialize and Deserialize BST——几乎所有树的面试题目都会回到BFS或者DFS,使用BFS,None节点存#
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 449 Serialize and Deserialize BST 序列化和反序列化二叉搜索树
详见:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ C++: /** * Definition fo ...
- LeetCode 428. Serialize and Deserialize N-ary Tree
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-n-ary-tree/ 题目: Serialization is the ...
随机推荐
- c# EF插入数据报错跟踪代码
我们在使用EF进行数据库插入的时候或出现一些插入失败的情况,但是具体是哪个字段不符合数据库设计要求无法得知,普通的try catch 无法捕获加上一下方法就可以 try { ...
- 给element添加自定义图标
element为我们提供了丰富的好用的组件,图标的样式也很多,但还是有一些常用的图标没有在官方图标库里边,比如说微信.淘宝.支付宝等等.那么如何把我们需要的图标添加到进去呢? 因为element有官方 ...
- Maven的仓库和settings.xml配置文件
(尊重劳动成果,转载请注明出处:https://blog.csdn.net/qq_25827845/article/details/83549846冷血之心的博客) 快速导航: Maven基础概念和安 ...
- kafka学习笔记(一)——概述
1.kafka是什么? 官方说明是: Kafka是一个分布式的基于发布/订阅模式的消息队列,主要应用于大数据实时处理领域. 提到了两个概念: 发布/订阅模式 消息队列 下边来理解一下这两个名词. 2. ...
- MVC学习笔记(四)---使用linq多表联查(SQL)
1.数据库原型(Students表中的ID和Scores表中的StudentID是对应的) 2.实现效果:查询出每个学生各个科目的成绩(用的是MVC学习笔记(三)—用EF向数据库中添加数据的架构) C ...
- mqtt client api: 阻塞API
fusesource版本:mqtt-client-1.11.jar下载地址:https://github.com/fusesource/mqtt-client fusesource提供三种mqtt c ...
- pathlib的使用
目录 一. Python2与Python3的路径处理的对比 二. pathlib的几个使用示例 1. 最简单的使用 2. 追加路径到Python的sys.path中 3. 生成目录树的技巧 4. 递归 ...
- jq对象才能使用jq方法,$(".a").eq(0) 和 $(”.a“)[0]
<a class="a"></a> <a class="a"></a> <a class="a& ...
- java Excel文件流输出(后台)没有下载提示
/**方案: *1. *问题源头应该是前端用的是ajax方式请求路径,要想出现下载Excel的弹窗,我个人理解应该是等同于重新打开一个窗口,而ajax默认不动页面直接将值返回,所以没有提升下载. *解 ...
- 教你如何配置linux用户实现禁止ssh登陆机器但可用sftp登录!
构想和目标最近有个这样的诉求:基于对线上服务器的保密和安全,不希望开发人员直接登录线上服务器,因为登录服务器的权限太多难以管控,如直接修改代码.系统配置,并且也直接连上mysql.因此希望能限制开发人 ...