您的位置:首页 > 编程语言 > C语言/C++

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
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;
}

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息