Leetcode: 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. Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
So the first question is: what is the difference between this and #297?
This here is BST, however, in #297, it's BT. "The encoded string should be as compact as possible" here. The special property of binary search trees compared to general binary trees allows a more compact encoding. So while the solutions to problem #297 still work here as well, they're not as good as they should be.
For general BT, to reconstruct the tree, we need the information of both in-order and pre-order, or in-order and post-order. We've practised that already.
However, as a BST, just the information of pre-order or post-order is sufficient to rebuild the tree.
The encoded string is also most compact, since we do not need to keep tract of information of 'Null nodes'.
Example: 4
2 6
1 3 5 7
The pre-order encoding is: "4213657". It is easy to tell "4" is root, "213" is left tree, "657" is right tree. We can use a Queue to implement this, very convenient.
/**
* 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) {
if (root == null) return "";
StringBuilder res = new StringBuilder();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode node = root;
while (!stack.isEmpty() || node!=null) {
if (node != null) {
res.append(node.val).append(" ");
stack.push(node);
node = node.left;
}
else {
node = stack.pop().right;
}
}
return res.toString().trim();
} // Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data==null || data.length()==0) return null;
String[] nodes = data.split(" ");
Queue<Integer> queue = new LinkedList<>();
for (String node : nodes) {
queue.offer(Integer.valueOf(node));
}
return buildTree(queue);
} public TreeNode buildTree(Queue<Integer> queue) {
if (queue.isEmpty()) return null;
TreeNode root = new TreeNode(queue.poll());
Queue<Integer> leftNodeVals = new LinkedList<>();
while (!queue.isEmpty() && queue.peek()<=root.val) {
leftNodeVals.offer(queue.poll());
}
root.left = buildTree(leftNodeVals);
root.right = buildTree(queue);
return root;
}
} // Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
Leetcode: Serialize and Deserialize BST的更多相关文章
- [LeetCode] 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 解题报告(Python)
[LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...
- [LeetCode] Serialize and Deserialize N-ary Tree N叉搜索树的序列化和去序列化
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
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
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...
- 【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] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
随机推荐
- 《DSP using MATLAB》示例Example5.7
代码: x = [1, 1, 1, 1, zeros(1,4)]; N = 8; % zero-padding operation X_DFT = dft(x,N); % DFT of x(n) ma ...
- 【bzoj1499】瑰丽华尔兹
这名字听起来实在有点耳熟..? 好吧去年暑假就应该过的题...切了 先注意到,天使施魔法的次数不限:我们可以使得某个时刻在特定方向移动一段距离(最长的长度为那个时间段)然后怎么来考虑这个DP: T,X ...
- Leetcode Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
- ZeroMQ接口函数之 :zmq_z85_decode – 从一个用Z85算法生成的文本中解析出二进制密码
ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_z85_decode zmq_z85_decode(3) ØMQ Manual - ØMQ/4.1 ...
- 有了门面,程序会更加体面!- pos软件基于三层架构 -09
续上篇) 大鸟说道:“实际上没有学过设计模式去理解三层架构会有失偏颇的,毕竟分层是更高一级别的模式,所谓的架构模式.不过在程序中,有意识的遵循设计原则,却也可以有效的做出好的设计.” ...
- 李洪强iOS经典面试题144-数据存储
李洪强iOS经典面试题144-数据存储 数据存储 sqlite中插入特殊字符的方法和接收到处理方法. 除'其他的都是在特殊字符前面加"/",而 ' -> '' .方法:k ...
- log4j详解(二)
在实际编程时,要使Log4j真正在系统中运行事先还要对配置文件进行定义.定义步骤就是对Logger.Appender及Layout的分别使用.Log4j支持两种配置文件格式,一种是XML格式的文件,一 ...
- oracle PL/SQL高级特性
触发器:存放在数据库中,并被隐含执行的存储过程. 由触发事件,触发条件,触发操作组成. DML触发器:指定触发器时机(before or after),触发事件(insert , delete, u ...
- PHP 模拟 HTTP 基本认证(Basic Authentication)
当某个页面需要认证才能进行访问时,接到请求后服务器端会在响应头中发送一个 WWW-Authenticate 首部(用来标识认证安全域),语法为 WWW-Authenticate:Basic relam ...
- jdk(多版本)安装注意!
♣安装jdk和jre ♣jdk配置环境变量和测试 ♣安装多版本jdk和切换 ♣jdk下的jre和第二次安装的jre的区别 注意点: 1.jdk版本需要是64位 2.安装JDK 选择安装目录 安装过程中 ...