0
0
DSA Goprogramming~10 mins

Tree Traversal Preorder Root Left Right 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 print the root node's value first in preorder traversal.

DSA Go
func preorder(node *Node) {
    if node == nil {
        return
    }
    fmt.Println(node.[1])
}
Drag options to blanks, or click blank then click option'
ARight
BLeft
CValue
DParent
Attempts:
3 left
💡 Hint
Common Mistakes
Printing node.Left or node.Right instead of node.Value.
Trying to print node.Parent which may not exist.
2fill in blank
medium

Complete the code to recursively traverse the left subtree in preorder.

DSA Go
func preorder(node *Node) {
    if node == nil {
        return
    }
    fmt.Println(node.Value)
    preorder(node.[1])
}
Drag options to blanks, or click blank then click option'
ARight
BLeft
CParent
DValue
Attempts:
3 left
💡 Hint
Common Mistakes
Calling preorder on node.Right instead of node.Left.
Calling preorder on node.Value which is not a node.
3fill in blank
hard

Fix the error in the code to correctly traverse the right subtree in preorder.

DSA Go
func preorder(node *Node) {
    if node == nil {
        return
    }
    fmt.Println(node.Value)
    preorder(node.Left)
    preorder(node.[1])
}
Drag options to blanks, or click blank then click option'
ALeft
BValue
CParent
DRight
Attempts:
3 left
💡 Hint
Common Mistakes
Calling preorder on node.Value or node.Parent instead of node.Right.
Calling preorder on node.Left twice.
4fill in blank
hard

Fill both blanks to print the root node's value first and then recursively traverse the left subtree in preorder.

DSA Go
func preorder(node *Node) {
    if node == nil {
        return
    }
    fmt.Println(node.[1])
    preorder(node.[2])
}
Drag options to blanks, or click blank then click option'
AValue
BLeft
CRight
DParent
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right child calls.
Printing node.Left instead of node.Value.
5fill in blank
hard

Fill all three blanks to complete the preorder traversal visiting root, left, then right.

DSA Go
func preorder(node *Node) {
    if node == nil {
        return
    }
    fmt.Println(node.[1])
    preorder(node.[2])
    preorder(node.[3])
}
Drag options to blanks, or click blank then click option'
AValue
BLeft
CRight
DParent
Attempts:
3 left
💡 Hint
Common Mistakes
Calling preorder on node.Parent which is incorrect.
Mixing up left and right child calls.