[leetcode]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树
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 tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
Example:
You may serialize the following tree: 1
/ \
2 3
/ \
4 5 as"[1,2,3,null,null,4,5]"
Clarification: The above format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
思路:
1. To serialize, maintain a StringBuilder, using dfs to preorder traversal the tree
(1) if node is null, StringBuilder append ("#") and a splitter(",")
(2)otherwise, StringBuilder append (root.val) and a splitter(",")
2. To deserialize, mainatain a queue containing each node informaton, using corresponding preorder traversal to build a tree
代码:
/**
* 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) {
StringBuilder sb = new StringBuilder();
buildString(root, sb);
return sb.toString();
} private void buildString(TreeNode root, StringBuilder sb){
if(root == null){
sb.append("#").append(",");
}else{
// I use pre-order traversal: root, left, right
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 == null) return null;
String[]strArr = data.split(",");
Queue<String> queue = new LinkedList<>();
// put each item of strArr into queue
Collections.addAll(queue, strArr);
return buildTree(queue);
} private TreeNode buildTree(Queue<String> queue){
if(queue.isEmpty()) return null;
String s = queue.poll();
if(s.equals("#")) return null;
// to match serialize pre-order traversal
TreeNode root = new TreeNode(Integer.parseInt(s));
root.left = buildTree(queue);
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]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树的更多相关文章
- 7 Serialize and Deserialize Binary Tree 序列化及反序列化二叉树
原题网址:http://www.lintcode.com/zh-cn/problem/serialize-and-deserialize-binary-tree/# 设计一个算法,并编写代码来序列化和 ...
- [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
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the process of ...
- [leetcode]428. 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]297. Serialize and Deserialize Binary Tree一般二叉树的编解码
由于一般的前序遍历不能唯一的还原出原本你的二叉树,所以要改变一下: 记录二叉树的结构信息,也就是空节点用符号表示 一般的前序遍历只是记录了节点的前后顺序,通过记录空节点,每一层的结构就可以记录下来 解 ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- LC 297 Serialize and Deserialize Binary Tree
问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...
- LeetCode OJ 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化
序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据.请设计一个算法来实现二叉树 ...
随机推荐
- 【转】python两个 list 获取交集,并集,差集的方法
1. 获取两个list 的交集: #方法一: a=[2,3,4,5] b=[2,5,8] tmp = [val for val in a if val in b] print tmp #[2, 5] ...
- telnet-redis-quit
很多时候 telnet 完就无法退出了,ctrl+c 有时也无法退出后来找到了正确的命令 ctrl+] 然后在telnet 命令行输入 quit 就可以退出了 telnet Trying 10.168 ...
- day04-Python的流程控制
- linux删除某用户密码
1.清空一个linux用户密码 # passwd -d user1 passwd: password expiry information changed. 2.指定key登录 ssh port111 ...
- JS 60秒后重发送验证码
//settime($("#getPhoneCode"),60); function settime($obj, time) { if (time == 0) { $obj.att ...
- python3学习笔记四(列表1)
参考http://www.runoob.com/python3/python3-list.html 序列 python包含6种内建的序列:列表,元组,字符串,Unicode字符串,buffer对象和x ...
- Linux vim快捷键
1 替换 r 替换 先按r再按要替换的内容 2 按yy复制当前行 按p是粘贴 3 # add at 18-10-25 #-------------------------------- ...
- [UE4]Scroll Box带滚动条的容器
一.黑边,当可以往下滚动的时候,下边会出现黑边.当可以往上滚动的时候,上边也会出现黑边. Scroll Box.Style.Style:也可以自定义上下左右黑边的样式: 二.Scroll Box. ...
- css 实现多行文本末尾显示省略号
思路: 省略号使用绝对定位添加,开头部分避免突兀使用c3渐变背景颜色 <!DOCTYPE html> <html lang="en"> <head&g ...
- 涂抹mysql笔记-mysql性能调优和诊断
<>关键性指标1.IOPS(Input/Output operations Per Second)每秒处理的I/O请求次数:需要说明的一点,通常提到磁盘读写能力,比如形容它每秒读300M写 ...