Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'.

There is no limit of how you deserialize or serialize a binary tree, you only need to make sure you can serialize a binary tree to a string and deserialize this string to the original structure.

Example

An example of testdata: Binary tree {3,9,20,#,#,15,7},  denote the following structure:

    3
/ \
9 20
/ \
15 7

Our data serialization use bfs traversal. This is just for when you got wrong answer and want to debug the input.

You can use other method to do serializaiton and deserialization.

Solution:

 /**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
class Solution {
/**
* This method will be invoked first, you should design your own algorithm
* to serialize a binary tree which denote by a root node to a string which
* can be easily deserialized by your own "deserialize" method later.
*/
public String serialize(TreeNode root) {
String res = "";
if (root==null) return res; List<TreeNode> nodeList = new ArrayList<TreeNode>();
nodeList.add(root);
int index = 0;
res = Integer.toString(nodeList.get(0).val);
while (index<nodeList.size()){
TreeNode cur = nodeList.get(index);
if (cur==null){
res += ",#,#";
} else {
if (cur.left!=null){
nodeList.add(cur.left);
res += ","+cur.left.val;
} else res += ",#";
if (cur.right!=null){
nodeList.add(cur.right);
res += ","+cur.right.val;
} else res += ",#";
}
index++;
} return res;
} /**
* This method will be invoked second, the argument data is what exactly
* you serialized at method "serialize", that means the data is not given by
* system, it's given by your own serialize method. So the format of data is
* designed by yourself, and deserialize it here as you serialize it in
* "serialize" method.
*/
public TreeNode deserialize(String data) {
if (data.isEmpty()) return null;
String[] temp = data.split(",");
TreeNode root = new TreeNode(Integer.parseInt(temp[0]));
List<TreeNode> nodeList = new ArrayList<TreeNode>();
nodeList.add(root);
int cur = 0, next = 1;
while (cur<nodeList.size()){
TreeNode curNode = nodeList.get(cur);
if (curNode==null) cur++;
else {
int left = next;
int right = next+1;
if (next<temp.length){
if (temp[next].equals("#")) nodeList.add(null);
else {
TreeNode leftChild = new TreeNode(Integer.parseInt(temp[next]));
curNode.left = leftChild;
nodeList.add(leftChild);
}
}
if (next+1<temp.length){
if (temp[next+1].equals("#")) nodeList.add(null);
else {
TreeNode rightChild = new TreeNode(Integer.parseInt(temp[next+1]));
curNode.right = rightChild;
nodeList.add(rightChild);
}
}
next += 2;
cur++;
}
} return root;
}
}

LintCode-Serialization and Deserialization Of Binary Tree的更多相关文章

  1. Lintcode: Binary Tree Serialization (Serialization and Deserialization Of Binary Tree)

    Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...

  2. LintCode 7.Serialize and Deserialize Binary Tree(含测试代码)

    题目描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将 ...

  3. [LintCode] Binary Tree Serialization

    Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...

  4. [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化

    One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...

  5. 【LeetCode】Verify Preorder Serialization of a Binary Tree(331)

    1. Description One way to serialize a binary tree is to use pre-order traversal. When we encounter a ...

  6. LeetCode Verify Preorder Serialization of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ 题目: One way to ...

  7. LeetCode OJ 331. Verify Preorder Serialization of a Binary Tree

    One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...

  8. Verify Preorder Serialization of a Binary Tree

    One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...

  9. 331. Verify Preorder Serialization of a Binary Tree

    One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...

随机推荐

  1. 会"说话"的勒索病毒Cerber

    最近有个案子与勒索病毒有关,证物是个台式机,运行Windows 7 64bit操作系统,委托方是某高科技公司,希望能调查出事发的关键时间点.感染来源及途径.恶意程序文件名等相关信息. 在对证物计算机进 ...

  2. poj3274 哈希

    这题终于让我AC了,其过程之艰辛我不想再回忆了,看了各种代码,一定要注意指针空和非空的问题,再一个要注意边界. #include <stdio.h> #include <string ...

  3. 谈谈 React.js 的核心入门知识

    近来React.js变得越来越流行,本文就来谈一谈React.js的入门实践,通过分析一些常用的概念,以及提供一些入门 的最佳编程编程方式,仅供参考. 首先需要搞懂的是,React并不是一个框架,Re ...

  4. 修改 Ueditor 默认显示的字体大小

    默认字体为16px,有点大,为了美观而且一屏可以显示更多内容,可以修改为12px 打开:ueditor.all.min.js 我用的是压缩版 找到如下代码: body{margin:8px;font- ...

  5. atexit注册的函数是在main函数之后执行?

    跟atexit函数相识已久,man手册里对atexit的解释是这么一段: The atexit() function registers the given function to be called ...

  6. Javascript 补位运算符

    看到一个题目~~3.14得到是3,一下子有点蒙,之前在Javascript中完全没有见过~这个运算符.经查才知道~是补位运算符,但是跟原码反码补码又有点不同(在反码这一块,不记正负),好吧,大学的东西 ...

  7. 根据Ip获取城市帮助类

    思路构建 1.先通过本地的测IP地址库进行匹配 2.如果本地IP地址库存在此IP的城市信息,就直接返回,调用速度也快 3.如果本地没有对应的IP城市信息,必须通过调用网络的IP查询的API了,这里我使 ...

  8. EcShop二次开发系列教程–总纲

    EcShop作为老牌的B2C独立网店系统,功能非常全名,强大的文件.数据库缓存机制,保证前后台系统执行速度更快.系统平稳运行.但是过多的功能也或多或少的会影响到系统的整个效率,所有在使用EcShop搭 ...

  9. MongoDB 学习笔记(三)—— 修改器的使用

    通常文档只会有一部分数据要更新,所以使用修改器来操作文档极为高效. 小技巧:了解函数功能,不带括号即可.如:db.blog.update即可查看update函数的具体参数和方法体. $set修改器 & ...

  10. WPF DragDrop事件元素跟随

    前一段时间项目里面要实现一个鼠标拖动一个元素到另外一个元素上面并且赋值的功能,由于要在surface上运行,拖动的时候手指会挡住系统默认的拖动图标,导致用户意识不到自己是不是在拖动着东西,所以要解决这 ...