Serialize-and-Deserialize-BST

题目地址

LeetCode#449 Serialize and Deserialize BST

题目描述

  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 search tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure.

The encoded string should be as compact as possible.

Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

解题思路

  二叉搜索树的序列化与反序列化。

解题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
ostringstream os;
serialize(root, os);
return os.str();
}

// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
istringstream is(data);
return deserialize(is);
}
void serialize(TreeNode* root, ostringstream& os) {
if (!root) os << "# ";
else {
os << root->val << " ";
serialize(root->left, os);
serialize(root->right, os);
}
}
TreeNode* deserialize(istringstream& is) {
string val = "";
is >> val;
if (val == "#") return NULL;
TreeNode* node = new TreeNode(stoi(val));
node->left = deserialize(is);
node->right = deserialize(is);
return node;
}
};
0%