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. Discuz! X3 全新安装图文教程

    Discuz! 是腾讯旗下 Comsenz 公司推出的以社区为基础的专业建站平台,帮助网站实现一站式服务.让论坛(BBS).个人空间(SNS).门户(Portal).群组(Group).应用开放平台( ...

  2. mig_7series_v4_0_data_gen_chk

    mig_7series_v4_0_data_gen_chk `timescale 1ns / 1ps ///////////////////////////////////////////////// ...

  3. JAVA常用工具类异常处理

    1异常的定义 异常就是与我们编译相违背在过程中出现的逻辑或忘记一些赋值等等 分为编译时错误和运行时错误 运行时异常 我们一般处理的时Exception异常: 异常处理 异常处理可以通过关键字try,c ...

  4. Class 泛型

    Java Class 泛型的例子说明: http://blog.chinaunix.net/uid-1911213-id-3085866.html http://blog.163.com/sir_87 ...

  5. 学Python的原因

    先立个旗,不学会誓不为人!!!!!!!!!!! 一直以来总是三天打鱼,两天晒网的学习,但是在体制内混久了发现,失去了很多的东西,得到的确极其有限,总感觉这样的生活会失去意义. 寻找生活的激情,重新发现 ...

  6. 深入理解volatile

    volatile知识点 --------------------------------------------------------------------------- 1.volatile关键 ...

  7. Mysql相关问题集锦

    1:连接阿里云的服务器时,用navicate连接SSH时提示:或提示指到另一个IP从而进不去. SSH:expected key exchange group packet form server 解 ...

  8. DRF框架之 serializers 序列化组件

    1. 什么是序列化,其实在python中我们就学了序列化工具json工具,就是吧信息存为类字典形式 2. DRF框架自带序列化的工具: serializers 3. DRF框架 serializers ...

  9. Linux命令:pwd

    打印当前目录的完全路径. -L 打印路径包含符合路径 -P 打印路径不含符合路径. -LP,可能打印的不同,取决于你对进入当前目录的方式是通过符号链接进入,还是物理目录进入.如果是符号链接进入,则-L ...

  10. tanera笔记

    use bit operation int i = ...; if ((i & 0x4) != 0) { //倒数第三位是为1 } C++的友元类和友元函数实例 - winfu - 博客园 h ...