0
0
DSA Goprogramming~10 mins

BST Inorder Successor 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 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'
Aright
Bleft
Cparent
Dnext
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.
2fill in blank
medium

Complete 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'
AminValueNode
BmaxValueNode
CfindParent
DinorderPredecessor
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxValueNode will give the largest node, not successor.
Using inorderPredecessor is the opposite of successor.
3fill in blank
hard

Fix 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'
Anext
Bright
Cparent
Dleft
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.
4fill in blank
hard

Fill 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'
AminValueNode
BmaxValueNode
Cleft
Dright
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.
5fill in blank
hard

Fill 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'
Aval
B-1
CminValueNode
Dright
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.