- 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.
Example
Example 1:
Input:[2,1,3]
Output:[2,1,3]
Explanation:
2
/
1 3
Example 2:
Input:[1,#,2]
Output:[1,#,2]
Explanation:
1
2
解法1:
serialize()用preOrder遍歷,如果遇到空節點,就輸出”# “,否則就輸出val + " "。
deserialize()檢查節點爲空則輸出NULL,否則遞歸調用deserialize()爲當前節點的左節點和右節點。
注意:
1)我們以前學過光靠preOrder或postOrder或inOrder是不能決定一個Binary Tree的,這裏爲什麼可以呢?因爲這裏serialize()把該Binary Tree變成了全二叉樹或滿二叉樹。對於全二叉樹或滿二叉樹,preOrder/postOrder/inOrder是可以決定這棵樹的。
2) stringStream 很有用,自動把string按“ ”split成tokens。
/**
* 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:
string serialize(TreeNode * root) {
stringstream ss;
serialize(root, ss);
// cout<<"ss.str()="<<ss.str()<<endl;
return ss.str();
}
TreeNode * deserialize(string &data) {
stringstream ss(data);
return deserialize(ss);
}
private:
void serialize(TreeNode * root, stringstream & ss) {
if (!root) ss << "# ";
else {
ss << root->val << " ";
serialize(root->left, ss);
serialize(root->right, ss);
}
}
TreeNode * deserialize(stringstream & ss) {
string val = "";
ss >> val;
if (val == "#") return NULL;
// cout<<"val="<<val<<endl;
TreeNode * node = new TreeNode(stoi(val));
node->left = deserialize(ss);
node->right = deserialize(ss);
return node;
}
};
解法2:
serialize()和deserialize()都是按層次遍歷來處理。
注意:
1) if (!(ss >> val)) 說明ss的token已經處理完了。
/**
* 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:
string serialize(TreeNode * root) {
if (!root) return "";
stringstream ss;
queue<TreeNode *> q;
q.push(root);
while(!q.empty()) {
TreeNode * node = q.front();
q.pop();
if (node) {
ss << node->val << " ";
q.push(node->left);
q.push(node->right);
} else {
ss << "# ";
}
}
// cout<<"ss.str()="<<ss.str()<<endl;
return ss.str();
}
TreeNode * deserialize(string &data) {
if (data.empty()) return NULL;
stringstream ss;
ss << data; //stringstream ss(data) is also OK
queue<TreeNode *> q;
string val = "";
ss >> val; // the first token
TreeNode * resNode = new TreeNode(stoi(val));
TreeNode * curNode;
q.push(resNode);
while(!q.empty()) {
TreeNode * node = q.front();
q.pop();
if (!(ss >> val)) break; //ss reads to the end!
if (val != "#") {
curNode = new TreeNode(stoi(val));
q.push(curNode);
node->left = curNode;
}
if (!(ss >> val)) break; //ss reads to the end!
if (val != "#") {
curNode = new TreeNode(stoi(val));
q.push(curNode);
node->right = curNode;
}
}
return resNode;
}
};
代碼同步在
https://github.com/luqian2017/Algorithm