Serialize and Deserialize Binary Tree
2016-06-15 21:41
381 查看
题目描述:
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.
解题思路:
观察序列化的表示明显属于二叉树的层次遍历,因此使用队列
AC代码如下:
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
string ans;
if (root == NULL) return ans;
queue<TreeNode*> nodes;
nodes.push(root);
while (!nodes.empty()){
TreeNode* p = nodes.front();
nodes.pop();
if (p == NULL){
ans += ",null";
}
else{
stringstream stream;
stream << p->val;
if (ans.size() == 0){
ans += stream.str();
}
else{
ans += "," + stream.str();
}
nodes.push(p->left);
nodes.push(p->right);
}
}
int pos = ans.size() - 1;
for (; pos >= 0; --pos){
if (ans[pos] >= '0' && ans[pos] <= '9')
break;
}
return ans.substr(0, pos + 1);
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
vector<string> value_tmp = splitString(data);
if (value_tmp.size() == 0) return NULL;
queue<int> values;
for (int i = 0; i < value_tmp.size(); ++i){
if (value_tmp[i] == "null"){
values.push(INT_MIN);
}
else{
values.push(atoi(value_tmp[i].c_str()));
}
}
TreeNode* root = new TreeNode(values.front());
values.pop();
queue<TreeNode*> nodes;
nodes.push(root);
while (!values.empty() && !nodes.empty()){
TreeNode* p = nodes.front();
nodes.pop();
int leftValue = values.front();
values.pop();
if (leftValue != INT_MIN){
TreeNode* leftNode = new TreeNode(leftValue);
p->left = leftNode;
nodes.push(leftNode);
}
if (values.empty()) break;
int rightValue = values.front();
values.pop();
if (rightValue != INT_MIN){
TreeNode* rightNode = new TreeNode(rightValue);
p->right = rightNode;
nodes.push(rightNode);
}
}
return root;
}
private:
vector<string> splitString(const string& s)
{
vector<string> ans;
int len = s.length();
if (len == 0) return ans;
for (int i = 0; i < len;){
int pos = s.find(',', i);
if (pos != string::npos){
if (pos == i){
i = pos + 1;
continue;
}
else{
string strTemp = s.substr(i, pos - i);
ans.push_back(strTemp);
i = pos + 1;
}
}
else{
string strTemp = s.substr(i, len - i);
ans.push_back(strTemp);
break;
}
}
return ans;
}
};
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
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.
解题思路:
观察序列化的表示明显属于二叉树的层次遍历,因此使用队列
AC代码如下:
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
string ans;
if (root == NULL) return ans;
queue<TreeNode*> nodes;
nodes.push(root);
while (!nodes.empty()){
TreeNode* p = nodes.front();
nodes.pop();
if (p == NULL){
ans += ",null";
}
else{
stringstream stream;
stream << p->val;
if (ans.size() == 0){
ans += stream.str();
}
else{
ans += "," + stream.str();
}
nodes.push(p->left);
nodes.push(p->right);
}
}
int pos = ans.size() - 1;
for (; pos >= 0; --pos){
if (ans[pos] >= '0' && ans[pos] <= '9')
break;
}
return ans.substr(0, pos + 1);
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
vector<string> value_tmp = splitString(data);
if (value_tmp.size() == 0) return NULL;
queue<int> values;
for (int i = 0; i < value_tmp.size(); ++i){
if (value_tmp[i] == "null"){
values.push(INT_MIN);
}
else{
values.push(atoi(value_tmp[i].c_str()));
}
}
TreeNode* root = new TreeNode(values.front());
values.pop();
queue<TreeNode*> nodes;
nodes.push(root);
while (!values.empty() && !nodes.empty()){
TreeNode* p = nodes.front();
nodes.pop();
int leftValue = values.front();
values.pop();
if (leftValue != INT_MIN){
TreeNode* leftNode = new TreeNode(leftValue);
p->left = leftNode;
nodes.push(leftNode);
}
if (values.empty()) break;
int rightValue = values.front();
values.pop();
if (rightValue != INT_MIN){
TreeNode* rightNode = new TreeNode(rightValue);
p->right = rightNode;
nodes.push(rightNode);
}
}
return root;
}
private:
vector<string> splitString(const string& s)
{
vector<string> ans;
int len = s.length();
if (len == 0) return ans;
for (int i = 0; i < len;){
int pos = s.find(',', i);
if (pos != string::npos){
if (pos == i){
i = pos + 1;
continue;
}
else{
string strTemp = s.substr(i, pos - i);
ans.push_back(strTemp);
i = pos + 1;
}
}
else{
string strTemp = s.substr(i, len - i);
ans.push_back(strTemp);
break;
}
}
return ans;
}
};
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性