/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; */ class Solution { public: string SerializeCore(TreeNode *root) { if (root == NULL) return "#!"; return to_string(root->val)+"!"+ SerializeCore(root->left)+SerializeCore(root->right); } char* Serialize(TreeNode *root) { if (root == NULL) return NULL; string str = SerializeCore(root); return strdup(str.c_str()); } TreeNode* DeserializeCore(stringstream& ss) { string str; getline(ss, str, '!'); if (str == "#") return NULL; else { TreeNode* node = new TreeNode(stoi(str)); node->left = DeserializeCore(ss); node->right = DeserializeCore(ss); return node; } } TreeNode* Deserialize(char *str) { if (str == NULL) return NULL; stringstream ss(str); return DeserializeCore(ss); } };