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 when applied to this binary tree?
Tree structure:
1
/ \
2 3
/ \
4 5
The serialization uses preorder traversal with '#' for null nodes and commas as separators.
Tree structure:
1
/ \
2 3
/ \
4 5
The serialization uses preorder traversal with '#' for null nodes and commas as separators.
DSA Go
type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func serialize(root *TreeNode) string { if root == nil { return "#" } leftSerialized := serialize(root.Left) rightSerialized := serialize(root.Right) return fmt.Sprintf("%d,%s,%s", root.Val, leftSerialized, rightSerialized) } // Tree construction root := &TreeNode{1, &TreeNode{2, nil, nil}, &TreeNode{3, &TreeNode{4, nil, nil}, &TreeNode{5, nil, nil}}} fmt.Println(serialize(root))
Attempts:
2 left
💡 Hint
Remember preorder traversal visits root, then left subtree, then right subtree. Null children are marked with '#'.
✗ Incorrect
The serialization visits nodes in preorder: root, left, right. Null children are represented by '#'. So the output is "1,2,#,#,3,4,#,#,5,#,#".
❓ Predict Output
intermediate2:00remaining
Result of Deserializing a Serialized String
Given the serialized string "1,2,#,#,3,#,4,#,#", what is the value of the right child of the root after deserialization?
DSA Go
type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func deserialize(data string) *TreeNode { vals := strings.Split(data, ",") var index int var build func() *TreeNode build = func() *TreeNode { if vals[index] == "#" { index++ return nil } val, _ := strconv.Atoi(vals[index]) index++ node := &TreeNode{Val: val} node.Left = build() node.Right = build() return node } return build() } root := deserialize("1,2,#,#,3,#,4,#,#") fmt.Println(root.Right.Val)
Attempts:
2 left
💡 Hint
Trace the deserialization step by step, matching preorder nodes and nulls.
✗ Incorrect
The root is 1, left child is 2 with no children, right child is 3 with no left child and right child 4. So root.Right.Val is 3.
🧠 Conceptual
advanced1:30remaining
Understanding Null Markers in Serialization
Why is it necessary to include a special marker (like '#') for null nodes when serializing a binary tree using preorder traversal?
Attempts:
2 left
💡 Hint
Think about how the deserialization function knows when to stop building a subtree.
✗ Incorrect
Null markers tell the deserialization function where a child is missing, so it can correctly reconstruct the tree structure and distinguish left from right children.
❓ Predict Output
advanced2:00remaining
Output of Deserialization Followed by Serialization
What is the output of serializing the tree obtained by deserializing the string "1,#,2,#,3,#,#" using the given serialize and deserialize functions?
DSA Go
type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func serialize(root *TreeNode) string { if root == nil { return "#" } leftSerialized := serialize(root.Left) rightSerialized := serialize(root.Right) return fmt.Sprintf("%d,%s,%s", root.Val, leftSerialized, rightSerialized) } func deserialize(data string) *TreeNode { vals := strings.Split(data, ",") var index int var build func() *TreeNode build = func() *TreeNode { if vals[index] == "#" { index++ return nil } val, _ := strconv.Atoi(vals[index]) index++ node := &TreeNode{Val: val} node.Left = build() node.Right = build() return node } return build() } root := deserialize("1,#,2,#,3,#,#") fmt.Println(serialize(root))
Attempts:
2 left
💡 Hint
The deserialized tree is skewed to the right. Serialization should reproduce the same string.
✗ Incorrect
Deserializing then serializing returns the same preorder string with null markers, preserving the tree shape exactly.
🚀 Application
expert2:30remaining
Count Nodes from Serialized String Without Full Deserialization
Given a serialized binary tree string using preorder traversal with '#' for nulls, which approach correctly counts the total number of non-null nodes without building the tree?
Attempts:
2 left
💡 Hint
Think about how preorder traversal works and how nulls affect node counting.
✗ Incorrect
Using a stack to simulate traversal allows counting nodes correctly without building the tree. Simply counting non-'#' values may overcount if not careful.