解法一:recursion DFS
解法二:non-recursion BFS
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
if(!root) return "";
queue<TreeNode*> temp;
temp.push(root);
string res;
while(!temp.empty()){
TreeNode* t = temp.front(); temp.pop();
if(t==NULL) res+="null|";
else {
res+=to_string(t->val)+"|";
temp.push(t->left);
temp.push(t->right);
}
}
return res;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
if(data.empty()) return nullptr;
int start=0,end=0;
queue<TreeNode*> temp;
TreeNode* res;
int size = data.size();
int f=0;
while(end!=(size-1)){
end = data.find_first_of('|', start);
string t = data.substr(start, end-start);
if(temp.empty()){
TreeNode* node = new TreeNode(stoi(t));
res = node;
temp.push(node);
}else{
TreeNode* node = temp.front();
if(!f) {
f=1;
if(t!="null"){
node->left = new TreeNode(stoi(t));
temp.push(node->left);
}
}
else{
f=0;
if(t!="null"){
node->right = new TreeNode(stoi(t));
temp.push(node->right);
}
temp.pop();
}
}
start = end+1;
}
return res;
}
};
// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
use istringstream 优化
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
ostringstream out;
queue<TreeNode*> q;
if (root) q.push(root);
while (!q.empty()) {
TreeNode *t = q.front(); q.pop();
if (t) {
out << t->val << ' ';
q.push(t->left);
q.push(t->right);
} else {
out << "# ";
}
}
return out.str();
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
if (data.empty()) return nullptr;
istringstream in(data);
queue<TreeNode*> q;
string val;
in >> val;
TreeNode *res = new TreeNode(stoi(val)), *cur = res;
q.push(cur);
while (!q.empty()) {
TreeNode *t = q.front(); q.pop();
if (!(in >> val)) break;
if (val != "#") {
cur = new TreeNode(stoi(val));
q.push(cur);
t->left = cur;
}
if (!(in >> val)) break;
if (val != "#") {
cur = new TreeNode(stoi(val));
q.push(cur);
t->right = cur;
}
}
return res;
}
};
spend tons of time on this stupid bug
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
if(!root) return "";
queue<TreeNode*> temp;
temp.push(root);
string res;
while(!temp.empty()){
int s = temp.size();
for(int i=0;i<s;i++){
TreeNode* t = temp.front(); temp.pop();
if(t==NULL) res+="null|";
else {
res+=to_string(t->val)+"|";
temp.push(t->left);
temp.push(t->right);
}
}
}
return res;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
int size = data.size();
if(size==0) return NULL;
int start=0,end=0;
queue<TreeNode*> temp;
TreeNode* res;
int f=0;
while(end!=(size-1)){
int end = data.find_first_of('|', start);
string t = data.substr(start, end-start);
if(temp.empty()){
TreeNode* node = new TreeNode(stoi(t));
res = node;
temp.push(node);
}else{
TreeNode* node = temp.front();
if(!f) {
f=1;
if(t!="null"){
node->left = new TreeNode(stoi(t));
temp.push(node->left);
}
}
else{
f=0;
if(t!="null"){
node->right = new TreeNode(stoi(t));
temp.push(node->right);
}
temp.pop();
}
}
start = end+1;
}
return res;
}
};
// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));