剑指offer-61:序列化二叉树
目录
1题目描述
请实现两个函数,分别用来序列化和反序列化二叉树
2题目答案
序列化、反序列化
没做!!!!
class Solution {
vector<int> buf;
void dfs(TreeNode* p){
if(!p) buf.push_back(0x23333);
else{
buf.push_back(p -> val);
dfs(p -> left);
dfs(p -> right);
}
}
TreeNode* dfs2(int*& p){
if(*p == 0x23333){
++p;
return NULL;
}
TreeNode* res = new TreeNode(*p);
++p;
res -> left = dfs2(p);
res -> right = dfs2(p);
return res;
}
public:
char* Serialize(TreeNode *root) {
buf.clear();
dfs(root);
int *res = new int[buf.size()];
for(unsigned int i = 0; i < buf.size(); ++i) res[i] = buf[i];
return (char*)res;
}
TreeNode* Deserialize(char *str) {
int *p = (int*)str;
return dfs2(p);
}
};