0
0
DSA C++programming~20 mins

Serialize and Deserialize Binary Tree in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Tree Serialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Serialization with Preorder Traversal
What is the output of the serialization function using preorder traversal on this binary tree?

Tree structure:
1
/ \
2 3
/ \
4 5
DSA C++
#include <bits/stdc++.h>

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

void serialize(TreeNode* root, std::string& out) {
    if (!root) {
        out += "#,";
        return;
    }
    out += std::to_string(root->val) + ",";
    serialize(root->left, out);
    serialize(root->right, out);
}

int main() {
    TreeNode* root = new TreeNode(1);
    root->left = new TreeNode(2);
    root->right = new TreeNode(3);
    root->right->left = new TreeNode(4);
    root->right->right = new TreeNode(5);
    std::string result;
    serialize(root, result);
    std::cout << result << std::endl;
    return 0;
}
A"1,2,#,#,3,4,#,#,5,#,#,"
B"1,2,3,4,5,#,#,#,#,#,#,"
C"1,3,5,#,#,4,#,#,2,#,#,"
D"1,#,2,#,3,#,4,#,5,#,#,"
Attempts:
2 left
💡 Hint
Think about preorder traversal: root, left subtree, then right subtree. Use '#' to mark null children.
Predict Output
intermediate
2:00remaining
Result of Deserializing a Serialized String
Given the serialized string "1,2,#,#,3,4,#,#,5,#,#," representing a binary tree, what is the value of the root's right child's left child after deserialization?
DSA C++
#include <bits/stdc++.h>

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

TreeNode* deserialize(std::istringstream& in) {
    std::string val;
    if (!getline(in, val, ',')) return nullptr;
    if (val == "#") return nullptr;
    TreeNode* node = new TreeNode(std::stoi(val));
    node->left = deserialize(in);
    node->right = deserialize(in);
    return node;
}

int main() {
    std::string data = "1,2,#,#,3,4,#,#,5,#,#,";
    std::istringstream in(data);
    TreeNode* root = deserialize(in);
    std::cout << root->right->left->val << std::endl;
    return 0;
}
A2
B5
C3
D4
Attempts:
2 left
💡 Hint
Follow the preorder deserialization: root, left subtree, right subtree. The right child's left child is the node after 3 in preorder.
🧠 Conceptual
advanced
1:00remaining
Why Use '#' in Serialization?
Why do we use the special marker '#' in the serialized string of a binary tree?
ATo separate node values in the string
BTo represent null (empty) children in the tree
CTo mark the end of the serialized string
DTo indicate the root node of the tree
Attempts:
2 left
💡 Hint
Think about how to represent missing children in a binary tree serialization.
Predict Output
advanced
2:00remaining
Output of Deserialization with Incorrect Input
What error or output occurs when deserializing the string "1,2,#,3,#,#,#," using the given deserialize function?
DSA C++
#include <bits/stdc++.h>

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

TreeNode* deserialize(std::istringstream& in) {
    std::string val;
    if (!getline(in, val, ',')) return nullptr;
    if (val == "#") return nullptr;
    TreeNode* node = new TreeNode(std::stoi(val));
    node->left = deserialize(in);
    node->right = deserialize(in);
    return node;
}

int main() {
    std::string data = "1,2,#,3,#,#,#,";
    std::istringstream in(data);
    TreeNode* root = deserialize(in);
    std::cout << root->left->left->val << std::endl;
    return 0;
}
ACompilation error
B3
CSegmentation fault (runtime error)
D0
Attempts:
2 left
💡 Hint
Check if the input string matches the expected preorder serialization format with null markers.
🚀 Application
expert
3:00remaining
Number of Nodes After Deserialization
After deserializing the string "10,5,#,#,15,12,#,#,20,#,#," into a binary tree, how many nodes does the tree contain?
DSA C++
#include <bits/stdc++.h>

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

TreeNode* deserialize(std::istringstream& in) {
    std::string val;
    if (!getline(in, val, ',')) return nullptr;
    if (val == "#") return nullptr;
    TreeNode* node = new TreeNode(std::stoi(val));
    node->left = deserialize(in);
    node->right = deserialize(in);
    return node;
}

int countNodes(TreeNode* root) {
    if (!root) return 0;
    return 1 + countNodes(root->left) + countNodes(root->right);
}

int main() {
    std::string data = "10,5,#,#,15,12,#,#,20,#,#,";
    std::istringstream in(data);
    TreeNode* root = deserialize(in);
    std::cout << countNodes(root) << std::endl;
    return 0;
}
A5
B6
C4
D7
Attempts:
2 left
💡 Hint
Count all nodes including root and all children reconstructed from the serialized string.