0
0
DSA C++programming~10 mins

Serialize and Deserialize Binary Tree in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start serialization by checking if the root is null.

DSA C++
string serialize(TreeNode* root) {
    if (root == [1]) return "#";
    // rest of serialization code
}
Drag options to blanks, or click blank then click option'
ANULL
Bnullptr
C0
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or NULL instead of nullptr
Checking root == root which is always true
2fill in blank
medium

Complete the code to append the root value to the serialized string.

DSA C++
string serialize(TreeNode* root) {
    if (root == nullptr) return "#";
    return to_string(root->val) + [1] + serialize(root->left) + [1] + serialize(root->right);
}
Drag options to blanks, or click blank then click option'
A";"
B" "
C","
D"-"
Attempts:
3 left
💡 Hint
Common Mistakes
Using spaces or semicolons which complicate parsing
No separator causing concatenated values
3fill in blank
hard

Fix the error in the deserialize helper function to correctly parse the next token.

DSA C++
string nextToken;
getline(ss, [1], ',');
Drag options to blanks, or click blank then click option'
AnextToken
Btoken
Cvalue
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using an undeclared variable
Using the wrong variable name causing compilation errors
4fill in blank
hard

Fill both blanks to correctly build the tree nodes during deserialization.

DSA C++
if (nextToken == "#") return [1];
int val = stoi(nextToken);
TreeNode* node = new TreeNode(val);
node->left = deserializeHelper(ss);
node->right = [2];
return node;
Drag options to blanks, or click blank then click option'
Anullptr
BdeserializeHelper(ss)
Cnew TreeNode(val)
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Returning new nodes for null
Not calling deserializeHelper for right subtree
5fill in blank
hard

Fill all three blanks to complete the deserialize function using a stringstream.

DSA C++
TreeNode* deserialize(string data) {
    stringstream [1](data);
    return [2]([3]);
}
Drag options to blanks, or click blank then click option'
Ass
BdeserializeHelper
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names
Passing string instead of stringstream to helper