0
0
DSA C++programming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Inorder Successor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Find the inorder successor of a node in BST
What is the output of the following C++ code that finds the inorder successor of a node with value 15 in the BST?
DSA C++
#include <iostream>

struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

Node* minValueNode(Node* node) {
    Node* current = node;
    while (current && current->left != nullptr) {
        current = current->left;
    }
    return current;
}

Node* inorderSuccessor(Node* root, Node* p) {
    if (p->right != nullptr) {
        return minValueNode(p->right);
    }
    Node* succ = nullptr;
    while (root != nullptr) {
        if (p->val < root->val) {
            succ = root;
            root = root->left;
        } else if (p->val > root->val) {
            root = root->right;
        } else {
            break;
        }
    }
    return succ;
}

int main() {
    Node* root = new Node(20);
    root->left = new Node(10);
    root->right = new Node(30);
    root->left->left = new Node(5);
    root->left->right = new Node(15);
    Node* p = root->left->right; // Node with value 15
    Node* succ = inorderSuccessor(root, p);
    if (succ != nullptr) {
        std::cout << succ->val << std::endl;
    } else {
        std::cout << "null" << std::endl;
    }
    return 0;
}
A15
B20
Cnull
D30
Attempts:
2 left
💡 Hint
Remember inorder successor is the smallest node greater than the given node.
Predict Output
intermediate
2:00remaining
Inorder successor when node has right subtree
What will be printed by this C++ code when finding the inorder successor of node with value 10?
DSA C++
#include <iostream>

struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

Node* minValueNode(Node* node) {
    Node* current = node;
    while (current && current->left != nullptr) {
        current = current->left;
    }
    return current;
}

Node* inorderSuccessor(Node* root, Node* p) {
    if (p->right != nullptr) {
        return minValueNode(p->right);
    }
    Node* succ = nullptr;
    while (root != nullptr) {
        if (p->val < root->val) {
            succ = root;
            root = root->left;
        } else if (p->val > root->val) {
            root = root->right;
        } else {
            break;
        }
    }
    return succ;
}

int main() {
    Node* root = new Node(20);
    root->left = new Node(10);
    root->right = new Node(30);
    root->left->left = new Node(5);
    root->left->right = new Node(15);
    Node* p = root->left; // Node with value 10
    Node* succ = inorderSuccessor(root, p);
    if (succ != nullptr) {
        std::cout << succ->val << std::endl;
    } else {
        std::cout << "null" << std::endl;
    }
    return 0;
}
A5
Bnull
C20
D15
Attempts:
2 left
💡 Hint
If the node has a right child, the successor is the leftmost node in that right subtree.
🔧 Debug
advanced
2:00remaining
Identify the error in inorder successor code
What error will this code produce when trying to find the inorder successor of node with value 25?
DSA C++
#include <iostream>

struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

Node* minValueNode(Node* node) {
    Node* current = node;
    while (current && current->left != nullptr) {
        current = current->left;
    }
    return current;
}

Node* inorderSuccessor(Node* root, Node* p) {
    if (p->right != nullptr) {
        return minValueNode(p->right);
    }
    Node* succ = nullptr;
    while (root != nullptr) {
        if (p->val < root->val) {
            succ = root;
            root = root->left;
        } else if (p->val > root->val) {
            root = root->right;
        } else {
            break;
        }
    }
    return succ;
}

int main() {
    Node* root = new Node(20);
    root->left = new Node(10);
    root->right = new Node(30);
    root->right->left = new Node(25);
    root->right->right = new Node(35);
    Node* p = root->right->left; // Node with value 25
    Node* succ = inorderSuccessor(root, p);
    if (succ != nullptr) {
        std::cout << succ->val << std::endl;
    } else {
        std::cout << "null" << std::endl;
    }
    return 0;
}
Anull
B20
C30
DSegmentation fault
Attempts:
2 left
💡 Hint
Check if the node has a right child and what the successor should be.
Predict Output
advanced
2:00remaining
Inorder successor for the maximum node in BST
What will this code print when finding the inorder successor of the node with value 35 (maximum node)?
DSA C++
#include <iostream>

struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

Node* minValueNode(Node* node) {
    Node* current = node;
    while (current && current->left != nullptr) {
        current = current->left;
    }
    return current;
}

Node* inorderSuccessor(Node* root, Node* p) {
    if (p->right != nullptr) {
        return minValueNode(p->right);
    }
    Node* succ = nullptr;
    while (root != nullptr) {
        if (p->val < root->val) {
            succ = root;
            root = root->left;
        } else if (p->val > root->val) {
            root = root->right;
        } else {
            break;
        }
    }
    return succ;
}

int main() {
    Node* root = new Node(20);
    root->left = new Node(10);
    root->right = new Node(30);
    root->right->left = new Node(25);
    root->right->right = new Node(35);
    Node* p = root->right->right; // Node with value 35
    Node* succ = inorderSuccessor(root, p);
    if (succ != nullptr) {
        std::cout << succ->val << std::endl;
    } else {
        std::cout << "null" << std::endl;
    }
    return 0;
}
Anull
B35
C30
D25
Attempts:
2 left
💡 Hint
The maximum node has no inorder successor in BST.
🧠 Conceptual
expert
2:00remaining
Inorder successor concept in BST with parent pointers
Given a BST where each node has a parent pointer, which of the following is the correct approach to find the inorder successor of a node p?
AIf p has right child, successor is leftmost node in right subtree; else move up using parent pointers until a node is left child of its parent, then return that parent.
BIf p has left child, successor is rightmost node in left subtree; else move up using parent pointers until a node is right child of its parent, then return that parent.
CSuccessor is always the parent node regardless of children.
DIf p has no children, successor is the root of the BST.
Attempts:
2 left
💡 Hint
Think about inorder traversal order and how parent pointers help.