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 Preorder Serialization
What is the output of the following Java code that serializes a binary tree using preorder traversal with '#' as null marker and ',' as separator?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int v) { val = v; } } public class SerializeTree { public static String serialize(TreeNode root) { if (root == null) return "#"; return root.val + "," + serialize(root.left) + "," + serialize(root.right); } public static void main(String[] args) { 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); System.out.println(serialize(root)); } }
Attempts:
2 left
💡 Hint
Think about preorder traversal: root, left subtree, right subtree. Use '#' for null children.
✗ Incorrect
The serialization visits root first, then left subtree, then right subtree. Null children are marked with '#'. The tree structure is preserved in the string.
❓ Predict Output
intermediateOutput of Deserialization Result
Given the serialized string "1,2,#,#,3,4,#,#,5,#,#", what is the value of the right child's right child node after deserialization?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int v) { val = v; } } public class DeserializeTree { static int index = 0; public static TreeNode deserialize(String[] data) { if (index >= data.length || data[index].equals("#")) { index++; return null; } TreeNode node = new TreeNode(Integer.parseInt(data[index++])); node.left = deserialize(data); node.right = deserialize(data); return node; } public static void main(String[] args) { String serialized = "1,2,#,#,3,4,#,#,5,#,#"; String[] data = serialized.split(","); TreeNode root = deserialize(data); System.out.println(root.right.right.val); } }
Attempts:
2 left
💡 Hint
Trace the deserialization steps carefully following preorder order.
✗ Incorrect
The right child of root is 3, its right child is 5. So the output is 5.
🔧 Debug
advancedIdentify the Bug in Serialization Code
What error will occur when running this serialization code for a binary tree?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int v) { val = v; } } public class SerializeTree { public static String serialize(TreeNode root) { if (root == null) return "#"; return serialize(root.left) + "," + root.val + "," + serialize(root.right); } public static void main(String[] args) { TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(3); System.out.println(serialize(root)); } }
Attempts:
2 left
💡 Hint
Check the order of concatenation in the return statement.
✗ Incorrect
The code visits left subtree, then root, then right subtree, which is inorder traversal, not preorder. The question asks about serialization using preorder, so this output is not preorder serialization.
📝 Syntax
advancedSyntax Error in Deserialization Code
Which option contains the correct fix to this syntax error in deserialization code snippet?
DSA Java
public TreeNode deserialize(String[] data) {
if (data[index] == "#") {
index++
return null;
}
TreeNode node = new TreeNode(Integer.parseInt(data[index++]));
node.left = deserialize(data);
node.right = deserialize(data);
return node;
}Attempts:
2 left
💡 Hint
Look for missing punctuation causing syntax error.
✗ Incorrect
The missing semicolon after index++ causes a syntax error. Adding it fixes the syntax error. Other options fix different issues or are unrelated.
🚀 Application
expertNumber of Nodes After Deserialization
Given the serialized string "10,5,#,#,15,12,#,#,20,#,#", how many nodes does the deserialized binary tree contain?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int v) { val = v; } } public class DeserializeTree { static int index = 0; public static TreeNode deserialize(String[] data) { if (index >= data.length || data[index].equals("#")) { index++; return null; } TreeNode node = new TreeNode(Integer.parseInt(data[index++])); node.left = deserialize(data); node.right = deserialize(data); return node; } public static int countNodes(TreeNode root) { if (root == null) return 0; return 1 + countNodes(root.left) + countNodes(root.right); } public static void main(String[] args) { String serialized = "10,5,#,#,15,12,#,#,20,#,#"; String[] data = serialized.split(","); TreeNode root = deserialize(data); System.out.println(countNodes(root)); } }
Attempts:
2 left
💡 Hint
Count all non-null nodes in the tree after deserialization.
✗ Incorrect
The tree has nodes with values 10, 5, 15, 12, and 20. Nulls are not counted. Total nodes = 5.
