Complete the code to start serialization by checking if the root is null.
string serialize(TreeNode* root) {
if (root == [1]) return "#";
// rest of serialization code
}We check if the root pointer is nullptr to know if the tree or subtree is empty.
Complete the code to append the root value to the serialized string.
string serialize(TreeNode* root) {
if (root == nullptr) return "#";
return to_string(root->val) + [1] + serialize(root->left) + [1] + serialize(root->right);
}We use a comma to separate node values in the serialized string for easy splitting later.
Fix the error in the deserialize helper function to correctly parse the next token.
string nextToken; getline(ss, [1], ',');
The variable nextToken is used to store the next substring from the stream.
Fill both blanks to correctly build the tree nodes during deserialization.
if (nextToken == "#") return [1]; int val = stoi(nextToken); TreeNode* node = new TreeNode(val); node->left = deserializeHelper(ss); node->right = [2]; return node;
Return nullptr for null nodes and recursively call deserializeHelper for right subtree.
Fill all three blanks to complete the deserialize function using a stringstream.
TreeNode* deserialize(string data) {
stringstream [1](data);
return [2]([3]);
}Create a stringstream named ss from data and call deserializeHelper(ss) to build the tree.