Algorithms—297.Serialize and Deserialize Binary Tree
2015-10-28 11:14
393 查看
思路:根据条件描述
/**
* 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) {
List<TreeNode> list=new ArrayList<TreeNode>();
list.add(root);
StringBuffer sb=new StringBuffer();
while(list.size()!=0){
for(int i=0;i<list.size();i++){
TreeNode node=list.get(i);
if(node==null){
sb.append("null,");
}else{
sb.append(node.val);
sb.append(",");
list.add(node.left);
list.add(node.right);
}
list.remove(i);
i--;
}
}
String ans=sb.toString();
return ans.substring(0,sb.length()-1);
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
String[] strs=data.split(",");
if(strs[0].equals("null")){
return null;
}
TreeNode ans=new TreeNode(Integer.parseInt(strs[0]));
List<TreeNode> list=new ArrayList<TreeNode>();
list.add(ans);
int i=1;
while(i<strs.length){
List<TreeNode> sonList=new ArrayList<TreeNode>();
for(TreeNode node:list){
if(i>=strs.length){
return ans;
}
if(!strs[i].equals("null")){
TreeNode left=new TreeNode(Integer.parseInt(strs[i]));
node.left=left;
sonList.add(left);
}
i++;
if(i>=strs.length){
return ans;
}
if(!strs[i].equals("null")){
TreeNode right=new TreeNode(Integer.parseInt(strs[i]));
node.right=right;
sonList.add(right);
}
i++;
}
list=sonList;
}
return ans;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
/**
* 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) {
List<TreeNode> list=new ArrayList<TreeNode>();
list.add(root);
StringBuffer sb=new StringBuffer();
while(list.size()!=0){
for(int i=0;i<list.size();i++){
TreeNode node=list.get(i);
if(node==null){
sb.append("null,");
}else{
sb.append(node.val);
sb.append(",");
list.add(node.left);
list.add(node.right);
}
list.remove(i);
i--;
}
}
String ans=sb.toString();
return ans.substring(0,sb.length()-1);
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
String[] strs=data.split(",");
if(strs[0].equals("null")){
return null;
}
TreeNode ans=new TreeNode(Integer.parseInt(strs[0]));
List<TreeNode> list=new ArrayList<TreeNode>();
list.add(ans);
int i=1;
while(i<strs.length){
List<TreeNode> sonList=new ArrayList<TreeNode>();
for(TreeNode node:list){
if(i>=strs.length){
return ans;
}
if(!strs[i].equals("null")){
TreeNode left=new TreeNode(Integer.parseInt(strs[i]));
node.left=left;
sonList.add(left);
}
i++;
if(i>=strs.length){
return ans;
}
if(!strs[i].equals("null")){
TreeNode right=new TreeNode(Integer.parseInt(strs[i]));
node.right=right;
sonList.add(right);
}
i++;
}
list=sonList;
}
return ans;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
相关文章推荐
- ubuntu下部署Django Web的两种方法
- Django的标准库django.contrib包介绍
- god
- Goldengate monitor v11.1
- Category - 3
- google 上不去破解方法
- mongo 过滤 分组查询
- Django -- 入门
- 不不不!我要认真写博客!--Django的安装
- Django 的 国际化
- 用Django制作第一个网页
- Django学习之manage.py使用
- POJ 3274:Gold Balanced Lineup 做了两个小时的哈希
- POJ 3274:Gold Balanced Lineup 做了两个小时的哈希
- 南阳CCPC G题 Ancient Go
- beggo 模板函数
- Google play内购 Iab
- 深入解析Go语言的io.ioutil标准库使用
- DVFS Governor Interactive-choose_freq()函数解析
- Go语言的os包中常用函数初步归纳