[leetcode]449. Serialize and Deserialize BST序列化与反序列化BST
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.
思路
To makes it the most compact possible, since we just need the order the values were inserted, we do not need to account for null nodes in the string with "#" or "null".
Hence, the final string contains only the values and separators, which makes it the most compact possible.
1. 在encode时候,don't account for null nodes
2. 在decode时候,不再用queue来存整个treenode的信息,而是用大小为1的数组idx来track当前扫到哪一个node了, 用idx来拿到该node value值(即mock up a pass-by-address process)。通过BST的attribute (left < root < right)来build tree.
代码
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
buildString(root, sb);
return sb.toString();
}
private void buildString(TreeNode root, StringBuilder sb) {
if (root == null) return;
sb.append(root.val).append(",");
buildString(root.left, sb);
buildString(root.right, sb);
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data.length() == 0) return null;
// use idx to mockup a pass-by-address
int[] idx = new int[]{0};
return buildTree(data.split(","), idx, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
private TreeNode buildTree(String[] strArr, int[] idx, int min, int max) {
if (idx[0] == strArr.length) return null;
int val = Integer.parseInt(strArr[idx[0]]);
// Go back if out of boundary
if (val < min || val > max) return null;
TreeNode root = new TreeNode(val);
// move to next address
idx[0]++;
// corresponding to encode pre-order
root.left = buildTree(strArr, idx, min, val);
root.right = buildTree(strArr, idx, val, max);
return root;
}
}
[leetcode]449. Serialize and Deserialize BST序列化与反序列化BST的更多相关文章
- [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
原题链接在这里: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 ...
- [LeetCode] 297. Serialize and Deserialize Binary Tree 二叉树的序列化和反序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [leetcode]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 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 ...
随机推荐
- 使用ansible安装docker以及docker-compose
转自:https://www.cnblogs.com/jsonhc/p/7879028.html 环境三台centos7主机: master:192.168.101.14,node1:192.168. ...
- 使用pt-table-checksum及pt-table-sync校验复制一致性
一.简介 pt-table-checksum是percona-toolkit系列工具中的一个, 可以用来检测主. 从数据库中数据的一致性.其原理是在主库上运行, 对同步的表进行checksum, 记录 ...
- [PHP]json_encode增加options参数后支持中文
---------------------------------------------------------------------------------------------------- ...
- 多线程数据库查询(ADO)
ADO多线程数据库查询通常会出现3个问题: 1.CoInitialize 没有调用(CoInitialize was not called):所以,在使用任何dbGo对象前,必须手 调用CoIniti ...
- Delphi接口的底层实现
引言 接口是面向对象程序语言中一个很重要的元素,它被描述为一组服务的集合,对于客户端来说,我们关心的只是提供的服务,而不必关心服务是如何实现的:对于服务端的类来说,如果它想实现某种服务,实现与该服务相 ...
- mongoDB如何处理多对多关系
问题描述: 例如在关系数据库中有一个Team表,一个User表,两者是多对多的关系,即一个Team可以有多个User,一个User也可能属于多个Team,请问这样的关系在MongoDB中如何存储? 如 ...
- 吴裕雄 14-MySQL DELETE 语句
以下是 SQL DELETE 语句从 MySQL 数据表中删除数据的通用语法:DELETE FROM table_name [WHERE Clause]如果没有指定 WHERE 子句,MySQL 表中 ...
- PCA 降维
http://f.dataguru.cn/spark-751832-1-1.html 我们可以利用PCA算法将向量的维数降低,从而实现特征转化.具体原理在<机器学习>课程中有详细的讲述.故 ...
- macbook pro。已经连接上wifi,但是,不能上网的问题
有天,macbook pro关机后,再打开就上不了网了,后面网上看了,说是安装了 lantern出问题,我一想,有次关机lantern是被我强制关掉的.所以再次打开lantern就可以 上网了,然后正 ...
- Java中String的intern方法,javap&cfr.jar反编译,javap反编译后二进制指令代码详解,Java8常量池的位置
一个例子 public class TestString{ public static void main(String[] args){ String a = "a"; Stri ...