0
0
DSA Goprogramming~10 mins

Serialize and Deserialize Binary Tree in DSA Go - Interactive Practice

Choose your learning style9 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.