[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 二叉树的序列化与反序列化
序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据.请设计一个算法来实现二叉树 ...
随机推荐
- 什么是web前端开发?
Web前端开发工程师,主要职责是利用(X)HTML/CSS/JavaScript/Flash等各种Web技术进行客户端产品的开发.完成客户端程序(也就是浏览器端)的开发,开发JavaScript以及F ...
- 1.1.10 word2010图片另存为存到图片库里,怎么修改之
1. 打开一个文档,在图片上右击,图片另存为,在弹出的[保存文件]对话框中. 2.单击左上角的[组织]下拉列表"属性"按钮,在弹出的[图片 属性]对话框中,单击[包含文件夹]按钮, ...
- 第二章 C#语法基础(2.1 C#语言的数据类型一)
C#的数据类型 [案例]本案例实现3位评委给一位选手评分,通过键盘输入各位评委的打分,通过屏幕输出该选手的平均分. [案例目的] (1)掌握变量的定义方式; (2)掌握常用的数据类型; (3)掌握数据 ...
- SSB基准测试
SSB(Star Schema Benchmark)是麻省州立大学波士顿校区的研究人员定义的基于现实商业应用的数据模型,业界公认用来模拟决策支持类应用,比较公正和中立.学术界和工业界普遍采用它来评价决 ...
- DDR3初识
DDR3初识 选择2:1 ratio 意味用户总线宽度为DDR物理数据接口宽度的4倍.
- JVM优化系列之一(-Xss调整Stack Space的大小)
Java程序中,每个线程都有自己的Stack Space(堆栈).这个Stack Space不是来自Heap的分配.所以Stack Space的大小不会受到-Xmx和-Xms的影响,这2个JVM参数仅 ...
- [UE4]Scale Box:缩放容器
一.Scale Box只能有一个子控件,再拖放一控件进去是不行的. 二.Scale Box缩放保持长宽比例 三. Scale Box.Strectching.Strectch:拉伸设置. Scale ...
- JQuery miniui使用小记
1.renderer="onActionRenderer" 如我们需要在一行数据时加上删除操作时,就需要在每行上加上“删除”按钮,以下为实现代码: 列加载时主要的属性
- FragmentTabHost切换Fragment时保存状态,避免切换Fragment走onCreateView和onDestroyView方法;
FragmentTabHost这个控件每次切换Fragment,都会走Fragment的onCreateView和onDestroyView方法,多以每次切换都会创建和销毁Fragment实例,先来看 ...
- dubbo使用简介
---------------------------------------------------------------------------------------------------- ...