[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 ...
随机推荐
- javascript的DOM操作获取元素
一.document.getElementById() 根据Id获取元素节点 <div id="div1"> <p id="p1"> ...
- 04_JSX练习
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Erlang Error Records
1.No match of right hand value ... Erlang变量名需要以大写开头.
- happyxiaofan的程序员书单
转自 happyxiaofan 读书的看法 从15年7月至今,研究生期间读了不少书,读书让我学到了很多,也是提升技术能力的一个重要手段.可能很多人嫌读书太花时间,曾经的我一度也是这么认为的,觉得一 ...
- Kubernetes 1.8.x 全手动安装教程----转自Kubernetes中文社区(部分内容根据实验环境做了些修改,特此感谢Kubernetes中文社区)
Kubernetes 提供了许多云端平台与操作系统的安装方式,本章将以全手动安装方式来部署,主要是学习与了解 Kubernetes 创建流程.若想要了解更多平台的部署可以参考 Picking the ...
- RabbitMQ系列教程之六:远程过程调用(RPC)(转载)
RabbitMQ系列教程之六:远程过程调用(RPC) 远程过程调用(Remote Proceddure call[RPC]) (本实例都是使用的Net的客户端,使用C#编写) 在第二个教程中,我们学习 ...
- OCR技术浅探(转)
网址:https://spaces.ac.cn/archives/3785 OCR技术浅探 作为OCR系统的第一步,特征提取是希望找出图像中候选的文字区域特征,以便我们在第二步进行文字定位和第三步进行 ...
- msf客户端渗透(二):PDF漏洞、恶意网站、flash漏洞、IE漏洞、java漏洞、android漏洞、VBScript感染payload
这个漏洞利用只在XP上有效 利用pdf漏洞利用payload exploit生成一个pdf文件 传到被攻击机上 启动msf侦听 exploit -j XP上双击运行这个pdf时,kali获取到一个sh ...
- elastic search文档详解
在elastic search中文档(document)类似于关系型数据库里的记录(record),类型(type)类似于表(table),索引(index)类似于库(database). 文档一定有 ...
- js实现商品颜色尺码联动以及购买数量的选择
<script type="text/javascript"> $(function(){ //初始化点击第一个颜色 jquery $("#colors a: ...