0
0
DSA Goprogramming~20 mins

Serialize and Deserialize Binary Tree in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Tree Serialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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.
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))
A1,2,#,#,3,4,#,#,5,#,#
B1,2,#,#,3,4,#,#,5,#
C1,2,#,3,4,#,#,5,#,#
D1,2,#,#,3,4,#,5,#,#,#
Attempts:
2 left
💡 Hint
Remember preorder traversal visits root, then left subtree, then right subtree. Null children are marked with '#'.
Predict Output
intermediate
2: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)
A0
B4
C3
Dnil (no right child)
Attempts:
2 left
💡 Hint
Trace the deserialization step by step, matching preorder nodes and nulls.
🧠 Conceptual
advanced
1: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?
ATo indicate the end of the serialized string
BTo distinguish between left and right children during deserialization
CTo reduce the size of the serialized string
DTo mark nodes that have only one child
Attempts:
2 left
💡 Hint
Think about how the deserialization function knows when to stop building a subtree.
Predict Output
advanced
2: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))
A1,#,2,#,#
B1,#,2,#,3,#
C1,#,2,#,3,#,#,#
D1,#,2,#,3,#,#
Attempts:
2 left
💡 Hint
The deserialized tree is skewed to the right. Serialization should reproduce the same string.
🚀 Application
expert
2: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?
AUse a stack to simulate preorder traversal, increment count for each number, and skip '#' markers
BCount all values that are not '#' by splitting the string by commas
CCount the number of commas and subtract the number of '#' markers
DTraverse the string and increment count only when a number is followed by two '#' markers
Attempts:
2 left
💡 Hint
Think about how preorder traversal works and how nulls affect node counting.