采用的是先序遍历
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
deque<string> Slipt(const string &res,const string &des){
deque<string> re;
int left=0;
int right=0;
while((right=res.find(des,left))!=string::npos){
re.push_back(res.substr(left,right-left));
left=right+1;
}
return re;
}
public:
string serialize(TreeNode *root) {
if(root==nullptr)
return "#!";
string cur=root->val+"!";
cur+=serialize(root->left);
cur+=serialize(root->right);
return cur;
}
TreeNode *deserialize(deque<string> &hold){
string temp=hold.front();
hold.pop_front();
if(temp=="#")
return nullptr;
TreeNode *root = new TreeNode(stoi(temp));
root->left=deserialize(hold);
root->right=deserialize(hold);
return root;
}
TreeNode *deserialize(string data) {
if(data.empty())
return nullptr;
deque<string> hold=Slipt(data,"!");
return deserialize(hold);
}
};