contains category wise problems(data structures, competitive) of popular platforms.
View the Project on GitHub mayankdutta/category-wise-problems
297. Serialize and Deserialize Binary Tree
space
after every single digit (to notice single digit, two digit no.s)#
(to notice the null values)data >> val
moves string until space to val
at a time.out << root->val
moves string to out stream
.string serialize(TreeNode* root) {
ostringstream out;
pre(root, out);
return out.str();
}
TreeNode* deserialize(string data) {
istringstream in(data);
return fun(in);
}
void pre(TreeNode* root, ostringstream& out) {
if (root == nullptr) {
out << "# ";
}
else {
out << root->val << ' ';
pre(root->left, out);
pre(root->right, out);
}
}
TreeNode* fun(istringstream& data) {
string val;
data >> val;
if (val == "#")
return nullptr;
TreeNode* root = new TreeNode(stoi(val), fun(data), fun(data));
return root;
}