[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 ...
随机推荐
- 配置tomcat的开发环境
第一步:鼠标右键计算机->属性->高级系统设置,进去之后,点击环境变量,如下图所示: 第二步:开始配置tomcat的环境变量,新建系统变量名CATALINA_BASE,值tomcat的安装 ...
- Django 数据库的迁移
先数据库迁移的两大命令: python manage.py makemigrations & python manage.py migrate 前者是将model层转为迁移文件migratio ...
- 一例对一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性的解决
这个问题相信只要是做MVC的,都碰到过,也都知道错误的原因,就是触发了定义的实例字段校验规则.比如定义的不为空,但是为空了,或者定义的字段长度为50,但是超过50了. 可是有时虽然知道是这样,但是具体 ...
- 2339 3.1.1 Agri-Net 最短网络
Description 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享 ...
- 直接在浏览器运行jsx及高版本的js代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- js版MD5 (Message-Digest Algorithm)加密算法
/**** MD5 (Message-Digest Algorithm)* http://www.webtoolkit.info/***/ var MD5 = function (string) { ...
- 学Android开发的人可以去的几个网站
学Android开发的人可以去的几个网站 1.<IT蓝豹>Android开源项目分享平台国内非常好的一个Android开发者分享站,分享android所有特效,每天都有最新的Android ...
- RPN(region proposal network)之理解
在faster-r-cnn 中,因为引入rpn层,使得算法速度变快了不少,其实rpn主要作用预测的是 “相对的平移,缩放尺度”,rpn提取出的proposals通常要和anchor box进行拟合回归 ...
- ADO.Net 数据库 删除
删除数据库里的信息和之前增加,修改大同小异,其写法更加简单,也是把SQL语句写为删除语句 删除一条数据,只需要获取并接收到这条数据唯一的能够代表这条数据的信息,比如主键 代码演示: using Sys ...
- python爬取酒店信息练习
爬取酒店信息,首先知道要用到那些库.本次使用request库区获取网页,使用bs4来解析网页,使用selenium来进行模拟浏览. 本次要爬取的美团网的蚌埠酒店信息及其评价.爬取的网址为“http:/ ...