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 Using Preorder Traversal
What is the output of the following Rust code that serializes a binary tree using preorder traversal with '#' as null marker and ',' as separator?
DSA Rust
use std::collections::VecDeque; #[derive(Debug)] struct TreeNode { val: i32, left: Option<Box<TreeNode>>, right: Option<Box<TreeNode>>, } fn serialize(root: &Option<Box<TreeNode>>, output: &mut String) { match root { Some(node) => { output.push_str(&node.val.to_string()); output.push(','); serialize(&node.left, output); serialize(&node.right, output); } None => { output.push_str("#,"); } } } fn main() { let tree = Some(Box::new(TreeNode { val: 1, left: Some(Box::new(TreeNode { val: 2, left: None, right: None, })), right: Some(Box::new(TreeNode { val: 3, left: Some(Box::new(TreeNode { val: 4, left: None, right: None, })), right: Some(Box::new(TreeNode { val: 5, left: None, right: None, })), })), })); let mut output = String::new(); serialize(&tree, &mut output); print!("{}", output); }
Attempts:
2 left
💡 Hint
Think about preorder traversal: root, left subtree, right subtree. Null nodes are marked with '#'.
✗ Incorrect
The code visits nodes in preorder and appends their values followed by a comma. Null children are represented by '#,'. The final string ends with a comma.
❓ Predict Output
intermediateResult 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 node after deserialization?
DSA Rust
use std::collections::VecDeque; #[derive(Debug)] struct TreeNode { val: i32, left: Option<Box<TreeNode>>, right: Option<Box<TreeNode>>, } fn deserialize(data: &mut VecDeque<&str>) -> Option<Box<TreeNode>> { if let Some(val) = data.pop_front() { if val == "#" { return None; } let val_num = val.parse::<i32>().unwrap(); let left = deserialize(data); let right = deserialize(data); Some(Box::new(TreeNode { val: val_num, left, right })) } else { None } } fn main() { let serialized = "1,2,#,#,3,4,#,#,5,#,#,"; let mut data: VecDeque<&str> = serialized.split(',').filter(|s| !s.is_empty()).collect(); let root = deserialize(&mut data); if let Some(node) = root { if let Some(right) = node.right { if let Some(left_of_right) = right.left { print!("{}", left_of_right.val); } } } }
Attempts:
2 left
💡 Hint
Follow the deserialization order and tree structure carefully.
✗ Incorrect
The root's right child is 3, whose left child is 4. So the printed value is 4.
🔧 Debug
advancedIdentify the Bug in Serialization Code
What error will occur when running the following Rust serialization function for a binary tree?
DSA Rust
fn serialize(root: &Option<Box<TreeNode>>, output: &mut String) {
if root.is_none() {
output.push_str("#");
} else {
let node = root.as_ref().unwrap();
output.push_str(&node.val.to_string());
output.push(',');
serialize(&node.left, output);
serialize(&node.right, output);
}
}Attempts:
2 left
💡 Hint
Check how null nodes are represented and separated.
✗ Incorrect
Null nodes are marked with '#' but no comma is added after them, so the serialized string lacks proper separators, breaking deserialization.
🚀 Application
advancedNumber of Nodes After Deserialization
After deserializing the string "1,2,#,#,3,4,#,#,5,#,#," into a binary tree, how many nodes does the tree contain?
Attempts:
2 left
💡 Hint
Count all non-null nodes represented in the serialized string.
✗ Incorrect
The tree has nodes with values 1, 2, 3, 4, and 5, totaling 5 nodes.
🧠 Conceptual
expertWhy Use Preorder Traversal for Serialization?
Why is preorder traversal commonly used for serializing and deserializing binary trees?
Attempts:
2 left
💡 Hint
Think about the order nodes are visited and how that helps rebuild the tree.
✗ Incorrect
Preorder visits root first, so when deserializing, the root is known before its children, enabling correct tree reconstruction.
