Challenge - 5 Problems
Binary Tree Serialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
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; }
Attempts:
2 left
💡 Hint
Think about preorder traversal: root, left subtree, then right subtree. Use '#' to mark null children.
✗ Incorrect
The serialization uses preorder traversal. For each node, we add its value followed by a comma. For null children, we add '#,'. The output matches preorder: 1, then left subtree (2,#,#), then right subtree (3,4,#,#,5,#,#).
❓ Predict Output
intermediate2: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; }
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.
✗ Incorrect
The deserialization reconstructs the tree from preorder. The root is 1, its right child is 3, and 3's left child is 4.
🧠 Conceptual
advanced1:00remaining
Why Use '#' in Serialization?
Why do we use the special marker '#' in the serialized string of a binary tree?
Attempts:
2 left
💡 Hint
Think about how to represent missing children in a binary tree serialization.
✗ Incorrect
The '#' symbol is used to represent null children so that the structure of the tree can be fully reconstructed during deserialization.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Check if the input string matches the expected preorder serialization format with null markers.
✗ Incorrect
The input string is malformed and does not represent a valid preorder serialization. The deserialize function will create a node where it expects a null, leading to a missing node and a runtime error when accessing root->left->left->val.
🚀 Application
expert3: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; }
Attempts:
2 left
💡 Hint
Count all nodes including root and all children reconstructed from the serialized string.
✗ Incorrect
The tree nodes are 10 (root), 5 (left child), 15 (right child), 12 (left child of 15), and 20 (right child of 15). Total nodes = 5.