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 序列化与反序列化二叉树的更多相关文章

  1. 7 Serialize and Deserialize Binary Tree 序列化及反序列化二叉树

    原题网址:http://www.lintcode.com/zh-cn/problem/serialize-and-deserialize-binary-tree/# 设计一个算法,并编写代码来序列化和 ...

  2. [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 ...

  3. Leetcode 297. Serialize and Deserialize Binary Tree

    https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the process of ...

  4. [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 ...

  5. [leetcode]297. Serialize and Deserialize Binary Tree一般二叉树的编解码

    由于一般的前序遍历不能唯一的还原出原本你的二叉树,所以要改变一下: 记录二叉树的结构信息,也就是空节点用符号表示 一般的前序遍历只是记录了节点的前后顺序,通过记录空节点,每一层的结构就可以记录下来 解 ...

  6. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  7. LC 297 Serialize and Deserialize Binary Tree

    问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...

  8. 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 ...

  9. 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化

    序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据.请设计一个算法来实现二叉树 ...

随机推荐

  1. PythonStudy——字典的操作 Dictionary operation

    dic = {'a': 1, 'b': 2} print(dic) # 增: 字典名[key] = 值 => key已存在就是修改值,不存在就是新增值 dic['c'] = 3 print(di ...

  2. Python shelve 模块

    使用json或者pickle持久化数据,能dump多次,但load的话只能取到最新的dump, 因为先前的数据已经被后面dump的数据覆盖掉了. 如果想要实现dump多次不被覆盖,就可以想到使用she ...

  3. 第1章 Java语言概述--HelloWorld--环境搭建

    SE学什么 第1章 Java语言概述 第2章 基本语法 第3章 数组 第4章 面向对象编程(上) 第5章 面向对象编程(中) 第6章 面向对象编程(下) 第7章 异常处理 第8章 枚举类&注解 ...

  4. webpack 中,importloaders 配置项的含义

    importLoaders:用于配置「css-loader 作用于 @import 的资源之前」有多少个 loader. 0 => no loaders (default); 1 => p ...

  5. 第三章 C#程序结构[3.2 选择结构的应用(Windows窗体应用程序)(四)]

    [案例]设计一个顾客选购商品的系统.其中,顾客身份有两类,一类是VIP,另一类是普通会员:商品种类有3种.分别是上衣.裤子和鞋子.其中,VIP享受8折优惠和商店赠送的礼品,而普通会员都不享受.单击[确 ...

  6. nodejs教程 安装express及配置app.js文件的详细步骤

    来自:http://www.jb51.net/article/36710.htm   express.js是nodejs的一个MVC开发框架,并且支持jade等多种模板.下面简单来说说express的 ...

  7. OpenStack的八年之痒

    2010年10月,OpenStack发布了第一个版本:上个月,发布了它的第18个版本Rocky.几年前气氛火爆,如今却冷冷清清.Rocky版本宣布后,OpenStack群里也就出现了几篇简短的翻译过来 ...

  8. LTE学习笔记(一)——背景知识

    一.标准化组织 无线通信技术的演进离不开一些标准化组织. 1.ITU(International Telecommunication Union) 国际电信联盟,主要任务是制定标准,分配无线频谱资源, ...

  9. 涂抹mysql笔记-mysql字符集

    字符集:查看mysql数据库当前都支持哪些字符集:system@(none)>show character set;+----------+--------------------------- ...

  10. ASCS HA

    Please let us know what do you mean by "the PAS can not be accessed", what error did you f ...