Bird
Raised Fist0
DSA Goprogramming~10 mins

Serialize and Deserialize Binary Tree in DSA Go - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new node with the given value.

DSA Go
func NewNode(val int) *TreeNode {
    return &TreeNode{Val: [1]
}
Drag options to blanks, or click blank then click option'
Anil
BTreeNode{}
C0
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed value like 0 instead of the input parameter.
Returning nil instead of a new node.
2fill in blank
medium

Complete the code to append a string to the builder during serialization.

DSA Go
func serializeHelper(root *TreeNode, sb *strings.Builder) {
    if root == nil {
        sb.WriteString("#,")
        return
    }
    sb.WriteString(strconv.Itoa([1]))
    sb.WriteString(",")
    serializeHelper(root.Left, sb)
    serializeHelper(root.Right, sb)
}
Drag options to blanks, or click blank then click option'
Aroot.Left
B0
Croot.Val
Droot.Right
Attempts:
3 left
💡 Hint
Common Mistakes
Appending left or right child pointers instead of the node value.
Appending a fixed number like 0.
3fill in blank
hard

Fix the error in the deserialization code to correctly parse the next token.

DSA Go
func deserializeHelper(tokens *[]string, index *int) *TreeNode {
    if *index >= len(*tokens) {
        return nil
    }
    val := (*tokens)[*index]
    [1]
    if val == "#" {
        return nil
    }
    num, _ := strconv.Atoi(val)
    node := &TreeNode{Val: num}
    node.Left = deserializeHelper(tokens, index)
    node.Right = deserializeHelper(tokens, index)
    return node
}
Drag options to blanks, or click blank then click option'
Aindex++
B*index += 1
C(*index)++
D*index++
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid syntax like *index++.
Incrementing the pointer itself instead of the value.
4fill in blank
hard

Fill both blanks to complete the code that splits the serialized string and starts deserialization.

DSA Go
func deserialize(data string) *TreeNode {
    tokens := strings.Split(data, [1])
    index := 0
    return deserializeHelper(&tokens, [2])
}
Drag options to blanks, or click blank then click option'
A","
B"#"
C&index
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Splitting by wrong delimiter like "#".
Passing index instead of &index.
5fill in blank
hard

Fill all three blanks to complete the Serialize function that returns the serialized string.

DSA Go
func Serialize(root *TreeNode) string {
    var sb [1]
    serializeHelper(root, &sb)
    result := sb.[2]()
    return result[:len(result)-[3]]
}
Drag options to blanks, or click blank then click option'
Astrings.Builder
BString
CStringBuilder
DStringBuffer
EString()
FToString()
GLen()
HLength()
I1
J2
K3
L4
Attempts:
3 left
💡 Hint
Common Mistakes
Using Java-like classes such as StringBuilder or StringBuffer.
Calling non-existent methods like ToString().
Removing wrong number of characters from the end.

Practice

(1/5)
1. What is the main purpose of serialize and deserialize functions for a binary tree?
easy
A. Convert a binary tree into a string and back to a tree
B. Sort the nodes of a binary tree in ascending order
C. Calculate the height of a binary tree
D. Delete all nodes in a binary tree

Solution

  1. Step 1: Understand serialization

    Serialization means converting a binary tree into a string format so it can be stored or sent.
  2. Step 2: Understand deserialization

    Deserialization means converting the string back into the original binary tree structure.
  3. Final Answer:

    Convert a binary tree into a string and back to a tree -> Option A
  4. Quick Check:

    Serialize/Deserialize = Convert tree to string and back [OK]
Hint: Serialization means tree to string, deserialization reverses it [OK]
Common Mistakes:
  • Confusing serialization with sorting
  • Thinking serialization deletes nodes
  • Mixing serialization with tree height calculation
2. Which of the following Go function signatures correctly represents a deserialize function for a binary tree?
easy
A. func deserialize(root *TreeNode) string
B. func deserialize(data int) *TreeNode
C. func deserialize(data string) *TreeNode
D. func deserialize(data []int) int

Solution

  1. Step 1: Identify input and output types

    Deserialize takes a string (serialized data) and returns a pointer to the root node of the tree.
  2. Step 2: Match function signature

    func deserialize(data string) *TreeNode matches this: input is string, output is *TreeNode pointer.
  3. Final Answer:

    func deserialize(data string) *TreeNode -> Option C
  4. Quick Check:

    Deserialize input string, output tree node pointer [OK]
Hint: Deserialize input is string, output is tree root pointer [OK]
Common Mistakes:
  • Using int instead of string as input
  • Returning string instead of tree node
  • Confusing serialize and deserialize signatures
3. Given the following serialized string of a binary tree using preorder with '#' as null marker: "1,2,#,#,3,4,#,#,5,#,#", what is the inorder traversal output of the deserialized tree?
medium
A. [1, 3, 2, 4, 5]
B. [1, 2, 3, 4, 5]
C. [2, 4, 1, 3, 5]
D. [2, 1, 4, 3, 5]

Solution

  1. Step 1: Reconstruct tree from preorder string

    Preorder: 1 (root), 2 (left child), # (left null), # (right null), 3 (right child), 4 (left), #, #, 5 (right), #, #.
  2. Step 2: Draw tree and find inorder traversal

    Tree structure:
    1
    / \
    2 3
    / \
    4 5
    Inorder (left, root, right): 2, 1, 4, 3, 5
  3. Final Answer:

    [2, 1, 4, 3, 5] -> Option D
  4. Quick Check:

    Inorder traversal = [2,1,4,3,5] [OK]
Hint: Draw tree from preorder, then do left-root-right for inorder [OK]
Common Mistakes:
  • Mixing preorder and inorder traversal orders
  • Ignoring null markers '#' in deserialization
  • Incorrectly placing children nodes
4. Consider this Go code snippet for deserializing a binary tree. What is the bug?
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
        }
        node := &TreeNode{Val: vals[index]}
        index++
        node.Left = build()
        node.Right = build()
        return node
    }
    return build()
}
medium
A. TreeNode.Val should be converted from string to int
B. Index variable should be global, not local
C. Strings.Split should use space instead of comma
D. The build function should not be recursive

Solution

  1. Step 1: Check TreeNode.Val assignment

    vals[index] is a string, but TreeNode.Val expects an int. Conversion is missing.
  2. Step 2: Identify fix

    Use strconv.Atoi(vals[index]) to convert string to int before assigning.
  3. Final Answer:

    TreeNode.Val should be converted from string to int -> Option A
  4. Quick Check:

    String to int conversion missing [OK]
Hint: Convert string to int before assigning to node value [OK]
Common Mistakes:
  • Forgetting to convert string to int
  • Changing index scope unnecessarily
  • Misusing Split delimiter
5. You want to serialize a binary tree so that it can be stored and later deserialized exactly as it was. Which approach best handles trees with duplicate values and null nodes?
hard
A. Use inorder traversal without null markers
B. Use preorder traversal with a special marker for null nodes
C. Use postorder traversal ignoring null nodes
D. Store only node values in a list without structure

Solution

  1. Step 1: Understand serialization needs

    To reconstruct the exact tree, we must record node order and null positions.
  2. Step 2: Evaluate options

    Preorder with null markers records structure and values including duplicates. Inorder or postorder without nulls loses structure. Storing only values loses tree shape.
  3. Final Answer:

    Use preorder traversal with a special marker for null nodes -> Option B
  4. Quick Check:

    Preorder + null markers preserves structure and duplicates [OK]
Hint: Preorder + null markers saves full tree structure [OK]
Common Mistakes:
  • Ignoring null nodes in serialization
  • Using inorder without nulls loses tree shape
  • Assuming values alone define tree
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard