class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
string res;
dfs1(root, res);
return res;
}
void dfs1(TreeNode* root, string& res)
{
if (!root)
{
res += "#,";
return;
}
res += to_string(root->val) + ',';
dfs1(root->left, res);
dfs1(root->right, res);
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
int u = 0;
return dfs2(data, u);
}
TreeNode* dfs2(string &data, int &u)
{
if (data[u] == '#')
{
u += 2;
return NULL;
}
int t = 0;
bool is_minus = false;
while (data[u] != ',')
{
if (data[u] == '-') is_minus = true;
else t = t * 10 + data[u] - '0';
u ++ ;
}
u ++ ;
if (is_minus) t = -t;
auto root = new TreeNode(t);
root->left = dfs2(data, u);
root->right = dfs2(data, u);
return root;
}
};