Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to move to the left child in Morris Traversal.
DSA Go
current = root for current != nil { if current.Left == nil { fmt.Println(current.Val) current = current[1] } else { // Find the inorder predecessor } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Moving to .Left instead of .Right causes infinite loop.
Using .Parent or .Next which are not standard pointers in this context.
✗ Incorrect
In Morris Traversal, when the left child is nil, we print the current node and move to its right child.
2fill in blank
mediumComplete the code to find the inorder predecessor in Morris Traversal.
DSA Go
predecessor := current.Left for predecessor.Right != nil && predecessor.Right != current { predecessor = predecessor[1] }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Moving left instead of right causes incorrect predecessor.
Using .Parent or .Next which are not relevant here.
✗ Incorrect
The inorder predecessor is the rightmost node in the left subtree, so we move right until nil or current.
3fill in blank
hardFix the error in setting the thread link in Morris Traversal.
DSA Go
if predecessor.Right == nil { predecessor.Right = current current = current[1] }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Moving right causes infinite loop.
Using .Parent or .Next which are invalid pointers.
✗ Incorrect
After setting the thread, move to the left child to continue traversal.
4fill in blank
hardFill both blanks to remove the thread and move to the right child in Morris Traversal.
DSA Go
else { predecessor.Right = [1] fmt.Println(current.Val) current = current[2] }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting predecessor.Right to current causes infinite loop.
Moving left instead of right after removing thread.
✗ Incorrect
Remove the thread by setting predecessor.Right to nil, then move to current's right child.
5fill in blank
hardFill all three blanks to complete the Morris Traversal inorder function.
DSA Go
func morrisInorder(root *Node) {
current := root
for current != nil {
if current.Left == nil {
fmt.Println(current.Val)
current = current[1]
} else {
predecessor := current.Left
for predecessor.Right != nil && predecessor.Right != current {
predecessor = predecessor[2]
}
if predecessor.Right == nil {
predecessor.Right = current
current = current[3]
} else {
predecessor.Right = nil
fmt.Println(current.Val)
current = current.Right
}
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing left and right pointers causes incorrect traversal.
Not removing thread properly leads to infinite loops.
✗ Incorrect
Move right when no left child, move right to find predecessor, move left after threading.