Bird
Raised Fist0
DSA C++programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the inorder successor of a node in a Binary Search Tree (BST)?
easy
A. The node with the smallest key greater than the given node's key
B. The node with the largest key smaller than the given node's key
C. The left child of the given node
D. The right child of the given node

Solution

  1. Step 1: Understand inorder traversal order

    Inorder traversal visits nodes in ascending order of their keys in a BST.
  2. Step 2: Define inorder successor

    The inorder successor of a node is the next node visited after it in inorder traversal, which is the smallest node with a key greater than the current node.
  3. Final Answer:

    The node with the smallest key greater than the given node's key -> Option A
  4. Quick Check:

    Inorder successor = smallest greater key [OK]
Hint: Inorder successor is next bigger node in sorted order [OK]
Common Mistakes:
  • Confusing successor with predecessor
  • Thinking successor is always right child
  • Assuming successor is parent node
2. Which of the following C++ function signatures correctly represents a function to find the inorder successor of a BST node given the root and the node? Assume Node is a struct with int key, and left/right pointers.
easy
A. Node inorderSuccessor(Node root, Node p);
B. int inorderSuccessor(Node root, int p);
C. void inorderSuccessor(Node* root, Node* p);
D. Node* inorderSuccessor(Node* root, Node* p);

Solution

  1. Step 1: Analyze parameter types

    The function needs access to the BST root and the target node pointer, so pointers are required.
  2. Step 2: Analyze return type

    The function should return a pointer to the successor node or nullptr if none exists.
  3. Final Answer:

    Node* inorderSuccessor(Node* root, Node* p); -> Option D
  4. Quick Check:

    Use pointers for tree nodes and return pointer [OK]
Hint: Use pointers for tree nodes and return pointer [OK]
Common Mistakes:
  • Passing nodes by value instead of pointer
  • Returning void instead of Node*
  • Using int instead of Node* for node parameters
3. Given the BST below and the node with key 15, what is the inorder successor's key?

        20
       /  \
     10    30
       \     \
       15    40
medium
A. 10
B. 20
C. 30
D. 40

Solution

  1. Step 1: Locate node 15 and find successor

    Node 15 has no right child, so successor is the lowest ancestor whose left child is also ancestor of 15.
  2. Step 2: Identify successor in BST

    20 is the parent of 15's ancestor and 15 is in left subtree of 20, so successor is 20.
  3. Final Answer:

    20 -> Option B
  4. Quick Check:

    Successor of 15 is 20 [OK]
Hint: If no right child, go up to first bigger ancestor [OK]
Common Mistakes:
  • Choosing right child 30 incorrectly
  • Choosing 40 which is too far
  • Choosing 10 which is smaller
4. Consider this buggy C++ code snippet to find the inorder successor of a node in BST:

Node* inorderSuccessor(Node* root, Node* p) {
    if (p->right != nullptr) {
        Node* curr = p->right;
        while (curr->left != nullptr) {
            curr = curr->left;
        }
        return curr;
    }
    Node* succ = nullptr;
    while (root != nullptr) {
        if (p->key < root->key) {
            succ = root;
            root = root->left;
        } else {
            root = root->right;
        }
    }
    return succ;
}
What is the bug in this code?
medium
A. It does not check if p has a left child
B. It incorrectly updates succ when p->key == root->key
C. It does not handle the case when p is nullptr
D. It returns the wrong node when p has a right child

Solution

  1. Step 1: Check null pointer dereference

    The code immediately accesses p->right and later p->key without verifying if p is nullptr, leading to a segmentation fault.
  2. Step 2: Verify other logic

    Right subtree min is correctly found. Ancestor search properly sets succ only on left branches and handles equality by traversing right (null since no right child).
  3. Final Answer:

    It does not handle the case when p is nullptr -> Option C
  4. Quick Check:

    Missing p == nullptr check before dereference [OK]
Hint: Missing nullptr check for input node p [OK]
Common Mistakes:
  • Ignoring nullptr checks for p
  • Confusing left child check for successor
  • Assuming bug in right subtree handling
5. You have a BST with nodes having keys: 5, 3, 7, 2, 4, 6, 8. You want to find the inorder successor of node with key 7. Which of the following is the correct approach to find it efficiently?
hard
A. If node 7 has right child, successor is leftmost node in right subtree; else, go up to first ancestor larger than 7
B. Successor is always the right child of node 7
C. Successor is the parent of node 7 regardless of children
D. Successor is the left child of node 7

Solution

  1. Step 1: Check if node 7 has right child

    Node 7 has right child 8, so successor is leftmost node in right subtree, which is 8 itself.
  2. Step 2: If no right child, find first ancestor larger than node

    This step is for cases where right child is absent, not applicable here.
  3. Final Answer:

    If node 7 has right child, successor is leftmost node in right subtree; else, go up to first ancestor larger than 7 -> Option A
  4. Quick Check:

    Right child exists, successor = leftmost right subtree [OK]
Hint: Check right subtree first; else climb ancestors [OK]
Common Mistakes:
  • Assuming successor is always right child
  • Ignoring ancestor climbing when no right child
  • Confusing successor with parent node
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard