0
0
DSA C++programming~20 mins

BST Inorder Predecessor in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Inorder Predecessor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Inorder Predecessor Function in BST
What is the output of the following C++ code that finds the inorder predecessor of a given node in a BST?
DSA C++
struct Node {
    int data;
    Node* left;
    Node* right;
};

Node* inorderPredecessor(Node* root, Node* p) {
    Node* predecessor = nullptr;
    while (root) {
        if (p->data <= root->data) {
            root = root->left;
        } else {
            predecessor = root;
            root = root->right;
        }
    }
    return predecessor;
}

int main() {
    Node n1{20, nullptr, nullptr};
    Node n2{10, nullptr, nullptr};
    Node n3{30, nullptr, nullptr};
    Node n4{5, nullptr, nullptr};
    Node n5{15, nullptr, nullptr};

    n1.left = &n2; n1.right = &n3;
    n2.left = &n4; n2.right = &n5;

    Node* pred = inorderPredecessor(&n1, &n5);
    if (pred) {
        std::cout << pred->data << std::endl;
    } else {
        std::cout << "No predecessor" << std::endl;
    }
    return 0;
}
A20
B15
C10
DNo predecessor
Attempts:
2 left
💡 Hint
Think about the largest value smaller than the node 15 in the BST.
🧠 Conceptual
intermediate
1:30remaining
Understanding Inorder Predecessor in BST
In a Binary Search Tree (BST), which of the following best describes the inorder predecessor of a node?
AThe node with the largest value smaller than the given node's value
BThe node with the smallest value larger than the given node's value
CThe left child of the given node
DThe right child of the given node
Attempts:
2 left
💡 Hint
Inorder traversal visits nodes in ascending order.
🔧 Debug
advanced
2:00remaining
Identify the Error in Inorder Predecessor Code
What error does the following C++ code produce when trying to find the inorder predecessor of a node in a BST?
DSA C++
Node* inorderPredecessor(Node* root, Node* p) {
    if (!root) return nullptr;
    if (p->left) {
        Node* temp = p->left;
        while (temp->right) temp = temp->right;
        return temp;
    }
    Node* predecessor = nullptr;
    while (root) {
        if (p->data < root->data) {
            root = root->left;
        } else if (p->data > root->data) {
            predecessor = root;
            root = root->right;
        }
    }
    return predecessor;
}

// Called with p = nullptr
ANo error, returns nullptr
BCompilation error due to missing return statement
CInfinite loop in while loop
DSegmentation fault due to dereferencing nullptr
Attempts:
2 left
💡 Hint
Check what happens if p is nullptr.
Predict Output
advanced
2:00remaining
Output of Inorder Predecessor for Root Node
What is the output of the following code when finding the inorder predecessor of the root node in a BST?
DSA C++
struct Node {
    int data;
    Node* left;
    Node* right;
};

Node* inorderPredecessor(Node* root, Node* p) {
    if (p->left) {
        Node* temp = p->left;
        while (temp->right) temp = temp->right;
        return temp;
    }
    Node* predecessor = nullptr;
    while (root) {
        if (p->data <= root->data) {
            root = root->left;
        } else {
            predecessor = root;
            root = root->right;
        }
    }
    return predecessor;
}

int main() {
    Node n1{20, nullptr, nullptr};
    Node n2{10, nullptr, nullptr};
    Node n3{30, nullptr, nullptr};
    n1.left = &n2; n1.right = &n3;

    Node* pred = inorderPredecessor(&n1, &n1);
    if (pred) {
        std::cout << pred->data << std::endl;
    } else {
        std::cout << "No predecessor" << std::endl;
    }
    return 0;
}
A30
B10
CNo predecessor
D20
Attempts:
2 left
💡 Hint
Check if root has a left subtree and find its rightmost node.
🚀 Application
expert
2:30remaining
Number of Nodes with Inorder Predecessor in BST
Given a BST with nodes 5, 10, 15, 20, 25 inserted in that order, how many nodes have a valid inorder predecessor?
A4
B5
C3
D2
Attempts:
2 left
💡 Hint
The smallest node has no predecessor.