0
0
DSA C++programming~10 mins

BST Inorder Successor in DSA C++ - 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 node.

DSA C++
TreeNode* leftChild = node->[1];
Drag options to blanks, or click blank then click option'
Aparent
Bleft
Cright
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' will give the right child, not the left.
Using 'parent' or 'next' is incorrect as they are not standard BST node pointers.
2fill in blank
medium

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

DSA C++
while (current->[1] != nullptr) {
    current = current->[1];
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cparent
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Checking right child instead of left will find the maximum, not minimum.
Using parent or next pointers is incorrect for this traversal.
3fill in blank
hard

Fix the error in the code to find the inorder successor when the node has no right child.

DSA C++
TreeNode* successor = nullptr;
TreeNode* ancestor = root;
while (ancestor != node) {
    if (node->val < ancestor->val) {
        successor = ancestor;
        ancestor = ancestor->[1];
    } else {
        ancestor = ancestor->right;
    }
}
Drag options to blanks, or click blank then click option'
Aparent
Bright
Cnext
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Moving to right child instead of left will cause infinite loop or wrong successor.
Using parent or next pointers is invalid here without parent links.
4fill in blank
hard

Fill both blanks to complete the function that finds the inorder successor in a BST.

DSA C++
if (node->right != nullptr) {
    TreeNode* temp = node->right;
    while (temp->[1] != nullptr) {
        temp = temp->[2];
    }
    return temp;
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cparent
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' will move to the right subtree, not the leftmost node.
Using 'parent' or 'next' is incorrect without parent pointers.
5fill in blank
hard

Fill all three blanks to complete the full inorder successor function in a BST without parent pointers.

DSA C++
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* node) {
    if (node->right != nullptr) {
        TreeNode* temp = node->right;
        while (temp->[1] != nullptr) {
            temp = temp->[2];
        }
        return temp;
    }
    TreeNode* successor = nullptr;
    TreeNode* ancestor = root;
    while (ancestor != node) {
        if (node->val < ancestor->val) {
            successor = ancestor;
            ancestor = ancestor->[3];
        } else {
            ancestor = ancestor->right;
        }
    }
    return successor;
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cparent
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using right pointers instead of left to find minimum node.
Using parent or next pointers which are not available.
Incorrect traversal direction when searching for successor.