[leetcode]449. Serialize and Deserialize 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.
题目
序列化反序列化二叉搜索树(尽量紧凑)
思路
这个题最大不同是 “encoded string needs to be as compact as possible”. 即encode尽量紧凑
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.
那么我们做的一个优化是,
在encode的时候,不再将root == null的信息encode到string里
问题来了,在decode的时候,怎么读取root == null的结点信息呢?
用int[] idx = new int[]{0}去track当前处理的是String[] nodes 中的第几个node
利用BST的attribute
若 such node value < min || > max, 肯定越界,返null
代码
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;
/*Q: 为何不将 root == null 加到stringbuilder里?
Since we just need the order the values were inserted, you 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.
Q: 为何选择pre-order
preorder traversal 的时候, 对于当前节点,所有的左节点比他小,右节点比他大
*/
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;
//
int[] idx = new int[]{0};
idx[0] = 0;
return buildTree(data.split(","), idx, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
private TreeNode buildTree(String[] nodes, int[] idx, int min, int max) {
if (idx[0] == nodes.length) return null;
int val = Integer.parseInt(nodes[idx[0]]);
if (val < min || val > max) return null; // Go back if we are over the boundary
TreeNode cur = new TreeNode(val);
idx[0]++; // update current position
/*keep with encode pre-order*/
cur.left = buildTree(nodes, idx, min, val);
cur.right = buildTree(nodes, idx, val, max);
return cur;
}
}
[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
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...
- 449 Serialize and Deserialize BST 序列化和反序列化二叉搜索树
详见:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ C++: /** * Definition fo ...
- [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 ...
- Java实现 LeetCode 449 序列化和反序列化二叉搜索树
449. 序列化和反序列化二叉搜索树 序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建. 设计一个算法 ...
- 【leetcode-449】序列化和反序列化二叉搜索树
序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建. 设计一个算法来序列化和反序列化二叉搜索树. 对序列 ...
- 【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 ...
随机推荐
- 原生java读取存储为xml格式的数据,并存储到java bean里
一.举例读取的文件为:X-bond可交易债券信息_20180917.xml <?xml version="1.0" encoding="UTF-8"?&g ...
- 使用 Python 把多个 MP4 合成一个视频(转)
这两天群里有个小伙伴有一个需求, 就是把很多个视频文件 合并成一个. 期间也找了各种软件, 如格式工厂, 但是只能一次合成50个文件, 小伙伴有几千个文件需要合成, 太繁琐; 又比如会声会影, 这个剪 ...
- sql 替换字段中的部分字符,替换指定字符
把列中凡是有2011的全部修改成2014,如 lieming 里的201101131431改成201401131431,写法: update tab set lieming = replace(l ...
- 【380】python 获取列表排序后的索引列表
参考:Equivalent of Numpy.argsort() in basic python? - Stack Overflow 通过 enumerate 实现 [i for i,v in sor ...
- genymotion使用学习
1 安装 直接去其官网(https://www.genymotion.com/#!/download)下载安装包安装即可,安装中会附带安装VirtualBox. 2 注册 必须使用帐号登录后,方可下载 ...
- [namespace]PHP命名空间的动态访问 & 使用技巧
----------------------------------------------------------------------------------------------- /* | ...
- 判断页面是否添加了W3C声明
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Java 分页与原理(上)
Java web 实习需要用到分页技术 所以现在学习一下 做个记录 方便以后查阅 分类:传统分页技术 下拉式分页技术 起始位置(0)开始 查询(10条记录)
- Microsoft® SQL Server® 2012 功能包
Microsoft® SQL Server® 2012 功能包 http://www.microsoft.com/zh-cn/download/details.aspx?id=29065 Micros ...
- android开源项目框架大全:
android开源项目框架大全: 1.多页切换TabHost9 高仿网易云音乐客户端的Home页面切换Tabhost 高仿网易云音乐客户端的Home页面切换Tabhost,并且三角形是透明的,实现方式 ...