7 Serialize and Deserialize Binary Tree 序列化及反序列化二叉树
原题网址:http://www.lintcode.com/zh-cn/problem/serialize-and-deserialize-binary-tree/#
设计一个算法,并编写代码来序列化和反序列化二叉树。将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”。
如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉树序列化为一个字符串,并且可以将字符串反序列化为原来的树结构。
注意事项
There is no limit of how you deserialize or serialize a binary tree, LintCode will take your output of serialize
as the input of deserialize
, it won't check the result of serialize.
给出一个测试数据样例, 二叉树{3,9,20,#,#,15,7}
,表示如下的树结构:
3
/ \
9 20
/ \
15 7
我们的数据是进行BFS遍历得到的。当你测试结果wrong answer时,你可以作为输入调试你的代码。
你可以采用其他的方法进行序列化和反序列化。
- /**
- * Definition of TreeNode:
- * class TreeNode {
- * public:
- * int val;
- * TreeNode *left, *right;
- * TreeNode(int val) {
- * this->val = val;
- * this->left = this->right = NULL;
- * }
- * }
- */
- class Solution {
- public:
- /**
- * 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.
- */
- string serialize(TreeNode * root) {
- // write your code here
- if (root==NULL)
- {
- return "";//return NULL出错;
- }
- string result;
- //result=result+to_string(root->val);
- result=result+int2str(root->val);
- queue<TreeNode *> level;
- level.push(root->left);
- level.push(root->right);
- while(!level.empty())
- {
- TreeNode *temp=level.front();
- level.pop();
- if (temp!=NULL)
- {
- //result=result+","+to_string(temp->val);
- result=result+","+int2str(temp->val);
- level.push(temp->left);
- level.push(temp->right);
- }
- else
- {
- result=result+","+"#";
- }
- }
- int size=result.size();
- int id=size-;
- while(id>&&(result[id]=='#'||result[id]==','))
- {
- id--;
- }
- result.resize(id+);
- return result;
- }
- /**
- * 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.
- */
- TreeNode * deserialize(string &data) {
- // write your code here
- if (data.empty())
- {
- return NULL;
- }
- int size=data.size();
- int i=;
- int ro=;//根节点数值;
- while(data[i]!=','&&i<size)
- {
- char tm=data[i];
- ro=ro*+tm-'';
- i++;
- }
- TreeNode * root=new TreeNode(ro);
- queue<TreeNode *> level;
- TreeNode *index=root;
- bool isLeft=true;
- for (;i<size;i++)
- {
- if (data[i]==',')
- {
- continue;
- }
- else if (data[i]=='#')
- {
- if (isLeft)
- {
- //index->left=NULL;
- isLeft=false;
- }
- else
- {
- //index->right=NULL;
- if (!level.empty())
- {
- index=level.front();
- level.pop();
- }
- isLeft=true;
- }
- }
- else
- {
- int val=;
- while(i<size&&data[i]!=',') //注意不能写成data[i]!=','&&i<size,因为下标可能超出范围;
- {
- char temp=data[i];
- val=val*+temp-'';
- i++;
- }
- TreeNode * tempNode=new TreeNode(val);
- level.push(tempNode);
- if (isLeft)
- {
- index->left=tempNode;
- isLeft=false;
- }
- else
- {
- index->right=tempNode;
- if (!level.empty())
- {
- index=level.front();
- level.pop();
- }
- isLeft=true;
- }
- }
- }
- return root;
- }
- string int2str(int &i)
- {
- string str;
- stringstream stream;
- stream<<i;
- str=stream.str();//stream>>str;
- return str;
- }
- };
- /**
- * Definition of TreeNode:
- * class TreeNode {
- * public:
- * int val;
- * TreeNode *left, *right;
- * TreeNode(int val) {
- * this->val = val;
- * this->left = this->right = NULL;
- * }
- * }
- */
- class Solution {
- public:
- /**
- * 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.
- */
- string serialize(TreeNode * root) {
- // write your code here
- if (root==NULL)
- {
- return "";
- }
- string result;
- //result=result+to_string(root->val);
- result=result+int2str(root->val);
- queue<TreeNode *> level;
- if (root->left==NULL&&root->right!=NULL)
- {
- level.push(root);
- level.push(root->right);
- }
- else if (root->left!=NULL&&root->right==NULL)
- {
- level.push(root->left);
- level.push(root);
- }
- else if (root->left!=NULL&&root->right!=NULL)
- {
- level.push(root->left);
- level.push(root->right);
- }
- while(!level.empty())
- {
- TreeNode *temp=level.front();
- level.pop();
- if (temp!=root)
- {
- //result=result+","+to_string(temp->val);
- result=result+","+int2str(temp->val);
- if (temp->left!=NULL&&temp->right!=NULL)
- {
- level.push(temp->left);
- level.push(temp->right);
- }
- else if (temp->left==NULL&&temp->right!=NULL)
- {
- level.push(root);
- level.push(temp->right);
- }
- else if (temp->left!=NULL&&temp->right==NULL)
- {
- level.push(temp->left);
- level.push(root);
- }
- else
- {
- level.push(root);
- level.push(root);
- }
- }
- else
- {
- result=result+","+"#";
- }
- }
- int size=result.size();
- int id=size-;
- while(id>&&(result[id]=='#'||result[id]==','))
- {
- id--;
- }
- result.resize(id+);
- return result;
- }
- /**
- * 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.
- */
- TreeNode * deserialize(string &data) {
- // write your code here
- if (data.empty())
- {
- return NULL;
- }
- int size=data.size();
- int i=;
- int ro=;//根节点数值;
- while(data[i]!=','&&i<size)
- {
- char tm=data[i];
- ro=ro*+tm-'';
- i++;
- }
- TreeNode * root=new TreeNode(ro);
- queue<TreeNode *> level;
- TreeNode *index=root;
- bool isLeft=true;
- for (;i<size;i++)
- {
- if (data[i]==',')
- {
- continue;
- }
- else if (data[i]=='#')
- {
- if (isLeft)
- {
- //index->left=NULL;
- isLeft=false;
- }
- else
- {
- //index->right=NULL;
- if (!level.empty())
- {
- index=level.front();
- level.pop();
- }
- isLeft=true;
- }
- }
- else
- {
- int val=;
- while(i<size&&data[i]!=',') //注意不能写成data[i]!=','&&i<size,因为下标可能超出范围;
- {
- char temp=data[i];
- val=val*+temp-'';
- i++;
- }
- TreeNode * tempNode=new TreeNode(val);
- level.push(tempNode);
- if (isLeft)
- {
- index->left=tempNode;
- isLeft=false;
- }
- else
- {
- index->right=tempNode;
- if (!level.empty())
- {
- index=level.front();
- level.pop();
- }
- isLeft=true;
- }
- }
- }
- return root;
- }
- string int2str(int &i)
- {
- string str;
- stringstream stream;
- stream<<i;
- str=stream.str();//stream>>str;
- return str;
- }
- };
7 Serialize and Deserialize Binary Tree 序列化及反序列化二叉树的更多相关文章
- [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 ...
- [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 ...
- [LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)
描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉 ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- [LeetCode] Serialize and Deserialize Binary Tree
Serialize and Deserialize Binary Tree Serialization is the process of converting a data structure or ...
- LC 297 Serialize and Deserialize Binary Tree
问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...
- LeetCode——Serialize and Deserialize Binary Tree
Description: Serialization is the process of converting a data structure or object into a sequence o ...
- LintCode 7.Serialize and Deserialize Binary Tree(含测试代码)
题目描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将 ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
随机推荐
- 5.1_springboot2.x与安全(spring security)
1.简介 常见的两个安全框架shiro|spring security,这里只介绍spring security; Spring Security是针对Spring项目的安全框架,也是Spring B ...
- bias、variance与拟合之间的关系
Error = Bias^2 + Variance+Noise 误差的原因: 1.Bias反映的是模型在样本上的输出与真实值之间的误差,即模型本身的精准度,即算法本身的拟合能力. 2.Variance ...
- Ip HostName查询
https://iplist.cc/api // 在线ip hostname查询
- vue3环境搭建以及创建简单项目。
1.环境准备,以下都是我的版本.自己在官网上面下载需要的版本. 尝试了Python3.7.3在创建vue3项目时出现问题. node.js10.16.0, python2.7.16, yarn1.16 ...
- java运行字符串代码
本文链接:https://blog.csdn.net/junlong750/article/details/50945883
- 家庭-养老院模型理解IOC和DI
控制反转 IOC 1. 概念 应用内部不负责依赖对象的创建和维护, 由第三方负责, 这样控制权就由应用内部转移到外部容器, 控制权的转移就是所谓的反转. 2. 比喻 有一户家庭(应用)有个老人(依赖对 ...
- leetcode-90-子集②
题目描述: 方法一:回溯 class Solution: def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: nums.s ...
- duilib库分析1.消息流程分析
看下CWindowWnd类与CPaintManagerUI类是咋进行消息分发的吧. 1. 先看下CPaintManagerUI类的MessageLoop函数: void CPaintManagerUI ...
- SpringCloud学习笔记(九):SpringCloud Config 分布式配置中心
概述 分布式系统面临的-配置问题 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务.由于每个服务都需要必要的配置信息才能运行,所以一套集中式的.动 ...
- 十条服务器端优化Web性能的技巧总结
原文地址:http://www.jb51.net/yunying/452723.html 提高 web 应用的性能从来没有比现在更重要过.网络经济的比重一直在增长:全球经济超过 5% 的价值是在因特网 ...