Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the left subtree first in inorder traversal.
DSA Go
func inorder(node *Node) {
if node == nil {
return
}
inorder(node.[1])
fmt.Print(node.value, " ")
inorder(node.right)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling inorder on node.right first.
Calling inorder on node.parent which does not exist.
Skipping the left subtree.
✗ Incorrect
In inorder traversal, we first visit the left child, then the root, then the right child. So we call inorder on node.left first.
2fill in blank
mediumComplete the code to print the root value in inorder traversal.
DSA Go
func inorder(node *Node) {
if node == nil {
return
}
inorder(node.left)
fmt.Print(node.[1], " ")
inorder(node.right)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Printing node.left or node.right instead of node.value.
Printing node.parent which is not part of the node struct.
✗ Incorrect
In inorder traversal, after visiting the left subtree, we print the root node's value.
3fill in blank
hardFix the error in the inorder traversal code to correctly traverse the right subtree.
DSA Go
func inorder(node *Node) {
if node == nil {
return
}
inorder(node.left)
fmt.Print(node.value, " ")
inorder(node.[1])
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling inorder on node.left again instead of node.right.
Calling inorder on node.parent or node.root which do not exist.
✗ Incorrect
After printing the root, inorder traversal visits the right subtree, so we call inorder on node.right.
4fill in blank
hardFill both blanks to complete the inorder traversal function that prints nodes in left-root-right order.
DSA Go
func inorder(node *Node) {
if node == nil {
return
}
inorder(node.[1])
fmt.Print(node.[2], " ")
inorder(node.right)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right children.
Printing node.left or node.right instead of node.value.
✗ Incorrect
The first blank is 'left' to visit the left subtree first. The second blank is 'value' to print the node's data.
5fill in blank
hardFill all three blanks to complete the inorder traversal function that prints nodes in left-root-right order.
DSA Go
func inorder(node *Node) {
if node == nil {
return
}
inorder(node.[1])
fmt.Print(node.[2], " ")
inorder(node.[3])
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.parent which does not exist.
Swapping left and right children in recursive calls.
✗ Incorrect
The blanks are filled with 'left' to visit left subtree, 'value' to print node data, and 'right' to visit right subtree.