0
0
DSA Goprogramming~10 mins

BST Inorder Predecessor 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 return the left child of a BST node.

DSA Go
func getLeftChild(node *Node) *Node {
    return node.[1]
}
Drag options to blanks, or click blank then click option'
ALeft
BRight
CParent
DValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Right' instead of 'Left' to get the left child.
Trying to access 'Value' instead of child nodes.
2fill in blank
medium

Complete the code to find the maximum node in a BST subtree.

DSA Go
func findMax(node *Node) *Node {
    current := node
    for current.[1] != nil {
        current = current.[1]
    }
    return current
}
Drag options to blanks, or click blank then click option'
AParent
BRight
CLeft
DValue
Attempts:
3 left
💡 Hint
Common Mistakes
Going left instead of right to find maximum.
Checking 'Value' instead of child pointers.
3fill in blank
hard

Fix the error in the code to find the inorder predecessor when left subtree exists.

DSA Go
func inorderPredecessor(node *Node) *Node {
    if node.Left != nil {
        pred := node.Left
        for pred.[1] != nil {
            pred = pred.[1]
        }
        return pred
    }
    return nil
}
Drag options to blanks, or click blank then click option'
AValue
BParent
CLeft
DRight
Attempts:
3 left
💡 Hint
Common Mistakes
Going left instead of right inside left subtree.
Returning the left child directly without traversal.
4fill in blank
hard

Fill both blanks to find the inorder predecessor when no left subtree exists.

DSA Go
func inorderPredecessor(node *Node) *Node {
    if node.Left == nil {
        pred := node.[1]
        for pred != nil && node == pred.[2] {
            node = pred
            pred = pred.[1]
        }
        return pred
    }
    return nil
}
Drag options to blanks, or click blank then click option'
AParent
BLeft
CRight
DValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using Right instead of Left to check child relation.
Not moving up the tree using Parent pointer.
5fill in blank
hard

Fill all three blanks to complete the full inorder predecessor function.

DSA Go
func inorderPredecessor(node *Node) *Node {
    if node.Left != nil {
        pred := node.Left
        for pred.[1] != nil {
            pred = pred.[1]
        }
        return pred
    }
    pred := node.[2]
    for pred != nil && node == pred.[3] {
        node = pred
        pred = pred.[2]
    }
    return pred
}
Drag options to blanks, or click blank then click option'
ARight
BParent
CLeft
DValue
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing Left and Right pointers incorrectly.
Not handling both cases of predecessor properly.