LintCode-Serialization and Deserialization Of Binary Tree
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.
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的更多相关文章
- 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 ...
- LintCode 7.Serialize and Deserialize Binary Tree(含测试代码)
题目描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将 ...
- [LintCode] Binary Tree Serialization
Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...
- [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, ...
- 【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 ...
- LeetCode Verify Preorder Serialization of a Binary Tree
原题链接在这里:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ 题目: One way to ...
- 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, ...
- 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, ...
- 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, ...
随机推荐
- IOS基础——alloc、init和new方法
alloc:分配内存. init:初始化. new:代替上面两个函数:分配内存,并且初始化. 注意: 1.在实际开发中很少会用到new,一般创建对象时我们一般是 [[className alloc]i ...
- partial class 说明
C# 2.0 可以将类.结构或接口的定义拆分到两个或多个源文件中,在类声明前添加partial关键字即可. 例如:下面的PartialTest类 class PartialTest { string ...
- 解决:JS如何取得当前正在执行的function的名字
代码如下 function getFName(fn){ return (/^[\s\(]*function(?:\s+([\w$_][\w\d$_]*))?\(/).exec(fn.toString( ...
- ASP.NET中application对象
ASP.NET中application对象的使用. Application对象的应用 1.使用Application对象保存信息 (1).使用Application对象保存信息 Applicat ...
- PHP isset()与empty()的区别详解
通过对PHP语言的学习,应该知道它是基于函数的一款HTML脚本语言. 庞大的函数库支持着PHP语言功能的实现. 有关PHP函数isset()与empty()的相关用法. PHP的isset()函数 一 ...
- (转)PHP的语言结构和函数的区别
相信大家经常看到对比一些PHP应用中,说用isset() 替换 strlen(),isset比strlen执行速度快等. 例子: if ( isset($user) ) { //do some thi ...
- PHP+MYSQL会员系统的开发实例教程
本文通过一个简单的实例完成了完整的PHP+MySQL会员系统功能.是非常实用的一个应用.具体实现步骤如下: 一.会员系统的原理: 登陆-->判断-->保持状态(Cookie或Session ...
- Mongodb初学习--安装、试用
MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. 在MongoDB中数据被分组存储在数据集中,被称为一个集合(Collection ...
- jquery学习笔记(4)--实现table隔行变色以及单选框选中
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...
- EMVTag系列2《磁条等效数据》
Ø 57 磁条2等效数据 L: var. up to 19 -M(必备):此数据必须存在并提供给终端,终端在读应用数据过程中,如果没有读到必备数据,终端中止交易 按GB/T 17552,磁条2的数据 ...