[LeetCode 297] Serialize and Deserialize Binary Tree
2015-11-18 16:28
495 查看
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.
For example, you may serialize the following tree
as
just the same as how LeetCode OJ 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.
solution:
1. preorder traversal, use another helper class(StringToken) to split string, and record current position
2. level order traversal
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
helptoserialize(root, sb);
return sb.toString();
}
public void helptoserialize(TreeNode root, StringBuilder sb) {
if(root == null) {
sb.append("# ");
} else {
sb.append(root.val);
sb.append(" ");
helptoserialize(root.left, sb);
helptoserialize(root.right, sb);
}
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
TreeNode root = helptodeserialize(new StringToken(data, " "));
return root;
}
public TreeNode helptodeserialize(StringToken token) {
if(!token.hasMoreElement()) return null;
String t1 = token.nextElement();
if(t1.equals("#")) return null;
TreeNode root = new TreeNode(Integer.valueOf(t1));
root.left = helptodeserialize(token);
root.right = helptodeserialize(token);
return root;
}
class StringToken {
public int len = 0;
ArrayList<String> dataStrings = new ArrayList<>();
public StringToken (String data, String spliter) {
String[] datas = data.split(spliter);
for(int i=0;i<datas.length;i++) {
dataStrings.add(datas[i]);
}
}
public boolean hasMoreElement() {
return len<dataStrings.size();
}
public String nextElement() {
return dataStrings.get(len++);
}
}
}
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.
For example, you may serialize the following tree
1 / \ 2 3 / \ 4 5
as
"[1,2,3,null,null,4,5]",
just the same as how LeetCode OJ 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.
solution:
1. preorder traversal, use another helper class(StringToken) to split string, and record current position
2. level order traversal
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
helptoserialize(root, sb);
return sb.toString();
}
public void helptoserialize(TreeNode root, StringBuilder sb) {
if(root == null) {
sb.append("# ");
} else {
sb.append(root.val);
sb.append(" ");
helptoserialize(root.left, sb);
helptoserialize(root.right, sb);
}
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
TreeNode root = helptodeserialize(new StringToken(data, " "));
return root;
}
public TreeNode helptodeserialize(StringToken token) {
if(!token.hasMoreElement()) return null;
String t1 = token.nextElement();
if(t1.equals("#")) return null;
TreeNode root = new TreeNode(Integer.valueOf(t1));
root.left = helptodeserialize(token);
root.right = helptodeserialize(token);
return root;
}
class StringToken {
public int len = 0;
ArrayList<String> dataStrings = new ArrayList<>();
public StringToken (String data, String spliter) {
String[] datas = data.split(spliter);
for(int i=0;i<datas.length;i++) {
dataStrings.add(datas[i]);
}
}
public boolean hasMoreElement() {
return len<dataStrings.size();
}
public String nextElement() {
return dataStrings.get(len++);
}
}
}
相关文章推荐
- 命令行快速技巧:如何定位一个文件
- jquery+CSS实现的多级竖向展开树形TRee菜单效果
- cmd tree命令 以树形格式罗列文件
- JQuery中serialize()用法实例分析
- jQuery ajax serialize()方法的使用以及常见问题解决
- 推荐8款jQuery轻量级树形Tree插件
- tree 以树形格式罗列文件
- EasyUI Tree+Asp.net实现权限树或目录树导航的简单实例
- E3 tree 1.6在Firefox下显示问题的修复方法
- js树形控件脚本代码
- PHP的serialize序列化数据以及JSON格式化数据分析
- php序列化函数serialize() 和 unserialize() 与原生函数对比
- JQuery中serialize()、serializeArray()和param()方法示例介绍
- jQuery ajax serialize() 方法使用示例
- swing中Tree与滚动条用法实例分析
- Spark Decision Tree
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2