Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the minimum node in a BST subtree.
DSA Go
func minValueNode(node *Node) *Node {
current := node
for current.[1] != nil {
current = current.left
}
return current
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' will find the maximum, not minimum.
Using 'parent' or 'next' is incorrect as they are not child pointers.
✗ Incorrect
The minimum value node in a BST subtree is found by going as left as possible.
2fill in blank
mediumComplete the code to find the inorder successor when the node has a right child.
DSA Go
func inorderSuccessor(root, n *Node) *Node {
if n.right != nil {
return [1](n.right)
}
// other cases omitted
return nil
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxValueNode will give the largest node, not successor.
Using inorderPredecessor is the opposite of successor.
✗ Incorrect
If the node has a right child, the successor is the minimum node in the right subtree.
3fill in blank
hardFix the error in the loop to find the inorder successor when the node has no right child.
DSA Go
func inorderSuccessor(root, n *Node) *Node {
var successor *Node
current := root
for current != nil {
if n.val < current.val {
successor = current
current = current.[1]
} else {
current = current.right
}
}
return successor
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Moving right when node value is less will skip potential successors.
Using parent or next pointers is not applicable here.
✗ Incorrect
When searching for successor without right child, move left if node value is less than current.
4fill in blank
hardFill both blanks to complete the inorder successor function for all cases.
DSA Go
func inorderSuccessor(root, n *Node) *Node {
if n.right != nil {
return [1](n.right)
}
var successor *Node
current := root
for current != nil {
if n.val < current.val {
successor = current
current = current.[2]
} else {
current = current.right
}
}
return successor
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxValueNode or moving right in the loop causes wrong successor.
Not handling both cases leads to incorrect results.
✗ Incorrect
Use minValueNode for right subtree and move left in the loop to find successor without right child.
5fill in blank
hardFill all three blanks to complete a function that returns the inorder successor value or -1 if none.
DSA Go
func inorderSuccessorValue(root, n *Node) int {
successor := inorderSuccessor(root, n)
if successor != nil {
return successor.[1]
}
return [2]
}
func inorderSuccessor(root, n *Node) *Node {
if n.right != nil {
return [3](n.right)
}
var successor *Node
current := root
for current != nil {
if n.val < current.val {
successor = current
current = current.left
} else {
current = current.right
}
}
return successor
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the node pointer instead of value.
Returning 0 or other default instead of -1.
Using wrong function for right subtree.
✗ Incorrect
Return the value of successor node, -1 if none, and use minValueNode for right subtree.