[剑指offer] 61. 序列化二叉树
题目描述
请实现两个函数,分别用来序列化和反序列化二叉树
题目很差,没有测试样例。 默认是先序遍历序列化。
class Solution { public: vector<int> vec; void Ser(TreeNode *pNode) { if (!pNode) vec.push_back(0x12345); else { vec.push_back(pNode->val); Ser(pNode->left); Ser(pNode->right); } } TreeNode *Des(int *&p) { if (*p == 0x12345) { p++; return NULL; } TreeNode *res = new TreeNode(*p); p++; res->left = Des(p); res->right = Des(p); return res; } char *Serialize(TreeNode *root) { vec.clear(); Ser(root); int *res = new int[vec.size()]; for (int i = 0; i < vec.size(); i++) res[i] = vec[i]; return (char *)res; } TreeNode *Deserialize(char *str) { int *p = (int *)str; return Des(p); } };