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 serialization code for the given binary tree?
Tree structure:
1
/ \
2 3
/ \
4 5
Tree structure:
1
/ \
2 3
/ \
4 5
DSA Python
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def serialize(root): def helper(node): if not node: return 'N' return str(node.val) + ',' + helper(node.left) + ',' + helper(node.right) return helper(root) root = TreeNode(1, TreeNode(2), TreeNode(3, TreeNode(4), TreeNode(5))) print(serialize(root))
Attempts:
2 left
💡 Hint
Think about preorder traversal: root, left subtree, then right subtree. Use 'N' for null nodes.
✗ Incorrect
The serialization uses preorder traversal. Starting at root 1, then left child 2 (which has no children), then right child 3 with children 4 and 5. Null children are marked as 'N'.
❓ Predict Output
intermediateResult of Deserializing a Serialized String
Given the serialized string "1,2,N,N,3,4,N,N,5,N,N", what is the value of the right child of the root after deserialization?
DSA Python
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def deserialize(data): def helper(nodes): val = next(nodes) if val == 'N': return None node = TreeNode(int(val)) node.left = helper(nodes) node.right = helper(nodes) return node return helper(iter(data.split(','))) root = deserialize("1,2,N,N,3,4,N,N,5,N,N") print(root.right.val if root and root.right else None)
Attempts:
2 left
💡 Hint
The root's right child is the node after the left subtree in preorder.
✗ Incorrect
The root is 1. Its left child is 2. After finishing left subtree, next node is 3 which is the right child of root.
🔧 Debug
advancedIdentify the Error in Deserialization Code
What error will the following deserialization code produce when given the input "1,2,N,N,3,4,N,N,5,N,N"?
DSA Python
def deserialize(data): nodes = data.split(',') def helper(): val = nodes.pop(0) if val == 'N': return None node = TreeNode(int(val)) node.left = helper() node.right = helper() return node return helper() root = deserialize("1,2,N,N,3,4,N,N,5,N,N")
Attempts:
2 left
💡 Hint
Check how the list is consumed during recursion.
✗ Incorrect
Using pop(0) removes the first element each time, so the list shrinks correctly and recursion works without error.
❓ Predict Output
advancedOutput of Serialization Using Level Order Traversal
What is the output of the following serialization code using level order traversal (BFS) for the given binary tree?
Tree structure:
1
/ \
2 3
/ \
4 5
Tree structure:
1
/ \
2 3
/ \
4 5
DSA Python
from collections import deque def serialize(root): if not root: return '' result = [] queue = deque([root]) while queue: node = queue.popleft() if node: result.append(str(node.val)) queue.append(node.left) queue.append(node.right) else: result.append('N') return ','.join(result) root = TreeNode(1, TreeNode(2), TreeNode(3, TreeNode(4), TreeNode(5))) print(serialize(root))
Attempts:
2 left
💡 Hint
Level order visits nodes level by level, left to right. Use 'N' for missing children.
✗ Incorrect
The queue processes nodes in order: 1, then 2 and 3, then their children. Null children are marked as 'N'.
🧠 Conceptual
expertWhy Use Null Markers in Tree Serialization?
Why is it necessary to include null markers (like 'N') when serializing a binary tree using preorder traversal?
Attempts:
2 left
💡 Hint
Think about how the deserialization knows where left subtree ends and right subtree begins.
✗ Incorrect
Null markers show where branches end, allowing the deserialization to rebuild the exact tree shape.
