Challenge - 5 Problems
Binary Tree Serialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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
Tree structure:
1
/ \
2 3
/ \
4 5
DSA C
typedef struct Node {
int val;
struct Node* left;
struct Node* right;
} Node;
void serialize(Node* root, char* buffer) {
if (root == NULL) {
strcat(buffer, "#,");
return;
}
char valStr[12];
sprintf(valStr, "%d,", root->val);
strcat(buffer, valStr);
serialize(root->left, buffer);
serialize(root->right, buffer);
}
// Assume buffer is large enough and initially empty
// Example usage:
// Node* root = ... (tree above)
// char buffer[100] = "";
// serialize(root, buffer);
// printf("%s", buffer);Attempts:
2 left
💡 Hint
Think about preorder traversal: root, left subtree, then right subtree. Use '#' to mark null nodes.
✗ Incorrect
The serialization uses preorder traversal. It visits root (1), then left subtree (2), which has no children (#,#), then right subtree (3), then its left child (4) with no children (#,#), then right child (5) with no children (#,#). So the output is "1,2,#,#,3,4,#,#,5,#,#,".
❓ Predict Output
intermediateResulting Tree After Deserialization
Given the serialized string "1,2,#,#,3,4,#,#,5,#,#," what is the structure of the binary tree after deserialization?
DSA C
typedef struct Node {
int val;
struct Node* left;
struct Node* right;
} Node;
// Deserialization function reads values from the string and reconstructs the tree
// '#' means NULL node
// After deserialization, print the tree in inorder traversal (left, root, right)
// Expected output format: "2 -> 1 -> 4 -> 3 -> 5 -> null"Attempts:
2 left
💡 Hint
Remember inorder traversal visits left child, then root, then right child.
✗ Incorrect
The deserialized tree matches the original tree. Inorder traversal visits nodes in order: left subtree (2), root (1), left child of right subtree (4), right subtree root (3), right child of right subtree (5). So the output is "2 -> 1 -> 4 -> 3 -> 5 -> null".
🔧 Debug
advancedIdentify the Bug in Serialization Code
What error will this serialization code cause when run on a binary tree?
DSA C
void serialize(Node* root, char* buffer) {
if (root == NULL) {
strcat(buffer, "#"); // Missing comma here
return;
}
char valStr[12];
sprintf(valStr, "%d,", root->val);
strcat(buffer, valStr);
serialize(root->left, buffer);
serialize(root->right, buffer);
}Attempts:
2 left
💡 Hint
Check how null nodes are marked in the output string.
✗ Incorrect
The null nodes are marked with "#" but without a comma after it. This causes the serialized string to have missing commas between null markers, which breaks the format expected by deserialization.
❓ Predict Output
advancedOutput of Deserialization with Incorrect Input
What error or output results from deserializing the string "1,2,#,3,#,#,#,"?
DSA C
// Deserialization reads values separated by commas // '#' means NULL node // The string is malformed because it misses some null markers // The deserialization function expects a complete preorder serialization // What happens when it tries to build the tree?
Attempts:
2 left
💡 Hint
Check if the input string has enough markers for all nodes and null children.
✗ Incorrect
The input string is incomplete and does not provide enough null markers for all children. The deserialization function will try to read beyond the string and cause a runtime error (e.g., reading invalid memory or crashing).
🧠 Conceptual
expertWhy Use '#' in Serialization of Binary Trees?
Why is it necessary to include a special marker like '#' for null nodes when serializing a binary tree?
Attempts:
2 left
💡 Hint
Think about how the deserialization function knows where left and right children are missing.
✗ Incorrect
Including '#' for null nodes ensures the serialized string captures the exact shape of the tree, including where children are missing. Without it, deserialization cannot reconstruct the original tree uniquely.
