Bird
Raised Fist0
DSA Cprogramming~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 output
Given the BST and the node with value 15, what is the value of its inorder successor?
DSA C
struct Node {
    int val;
    struct Node* left;
    struct Node* right;
};

// BST structure:
//        20
//       /  \
//     10    30
//       \     \
//       15    40

// Node to find successor for: 15

// Inorder traversal: 10 -> 15 -> 20 -> 30 -> 40

// Inorder successor of 15 is the next node in inorder traversal.
A20
B30
C15
D10
Attempts:
2 left
💡 Hint
Inorder successor is the next node in the inorder traversal sequence.
❓ Predict Output
intermediate
2:00remaining
Output of inorder successor function when node has right subtree
What will be the output of the inorder successor function for node with value 10 in the given BST?
DSA C
struct Node {
    int val;
    struct Node* left;
    struct Node* right;
};

// BST structure:
//        10
//       /  \
//      5    15
//           / \
//         12  20

// Node to find successor for: 10

// Inorder traversal: 5 -> 10 -> 12 -> 15 -> 20

// Inorder successor of 10 is the leftmost node in right subtree.
A15
B12
C20
D5
Attempts:
2 left
💡 Hint
If node has right child, successor is leftmost node in right subtree.
🔧 Debug
advanced
2:00remaining
Identify the error in inorder successor code snippet
What error will this code produce when trying to find the inorder successor of a node in a BST?
DSA C
struct Node* inorderSuccessor(struct Node* root, struct Node* p) {
    struct Node* successor = NULL;
    while (root != NULL) {
        if (p->val < root->val) {
            successor = root;
            root = root->left;
        } else if (p->val > root->val) {
            root = root->right;
        } else {
            if (root->right != NULL) {
                root = root->right;
                while (root->left != NULL) {
                    root = root->left;
                }
                return root;
            }
            break;
        }
    }
    return successor;
}

// Error: What happens if p is NULL?
AReturns NULL safely
BCompilation error due to missing return
CSegmentation fault due to dereferencing NULL pointer
DInfinite loop
Attempts:
2 left
💡 Hint
Check if p is NULL before accessing p->val.
🧠 Conceptual
advanced
2:00remaining
Inorder successor when node has no right subtree
If a node in a BST has no right child, how do you find its inorder successor?
AThe lowest ancestor of the node whose left child is also ancestor of the node
BThe rightmost node in the left subtree
CThe parent node always
DThe leftmost node in the right subtree
Attempts:
2 left
💡 Hint
Think about climbing up the tree to find a node greater than the current node.
🚀 Application
expert
2:00remaining
Number of nodes with no inorder successor in BST
Given a BST with unique values, how many nodes have no inorder successor?
AAll nodes with no right child
BAll leaf nodes
CExactly one node, the minimum valued node
DExactly one node, the maximum valued node
Attempts:
2 left
💡 Hint
Inorder successor is next higher value node; who has none?

Practice

(1/5)
1. What is the inorder successor of a node in a Binary Search Tree (BST)?
easy
A. The node with the largest key smaller than the given node's key
B. The node with the smallest key greater 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 in BST

    Inorder traversal visits nodes in ascending order of their keys.
  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 greater than it.
  3. Final Answer:

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

    Inorder successor = next bigger node [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 left child
2. Which of the following C function signatures correctly represents a function to find the inorder successor of a BST node?
easy
A. struct Node* inorderSuccessor(struct Node* root, struct Node* n);
B. int inorderSuccessor(int root, int n);
C. void inorderSuccessor(struct Node* root);
D. struct Node inorderSuccessor(struct Node n);

Solution

  1. Step 1: Analyze function parameters

    The function needs the BST root and the node to find successor for, both as pointers.
  2. Step 2: Analyze return type

    It should return a pointer to the successor node, so return type is struct Node*.
  3. Final Answer:

    struct Node* inorderSuccessor(struct Node* root, struct Node* n); -> Option A
  4. Quick Check:

    Correct signature uses pointers for nodes [OK]
Hint: Use pointers for nodes and return pointer to successor [OK]
Common Mistakes:
  • Using int instead of struct Node*
  • Missing node parameter
  • Returning struct Node by value
3. Given the BST below and the node with value 15, what is the inorder successor?

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

Solution

  1. Step 1: Locate node 15 and find its right subtree

    Node 15 has no right child, so successor is an ancestor.
  2. Step 2: Move up to find the lowest ancestor greater than 15

    Parent 10 is less than 15, move up to 20 which is greater than 15.
  3. Final Answer:

    20 -> Option C
  4. Quick Check:

    Next bigger after 15 is 20 [OK]
Hint: If no right child, successor is nearest bigger ancestor [OK]
Common Mistakes:
  • Choosing right child 30 incorrectly
  • Choosing smaller ancestor 10
  • Confusing successor with predecessor
4. Consider this buggy C code snippet to find the inorder successor of a node in BST:

struct Node* inorderSuccessor(struct Node* root, struct Node* n) {
    if (n->right != NULL) {
        struct Node* temp = n->right;
        while (temp->left != NULL) {
            temp = temp->left;
        }
        return temp;
    }
    struct Node* succ = NULL;
    while (root != NULL) {
        if (n->data < root->data) {
            succ = root;
            root = root->left;
        } else {
            root = root->right;
        }
    }
    return succ;
}
What is the main logical error in this code?
medium
A. It incorrectly updates successor when moving right
B. It returns the wrong node when node has right child
C. It uses a while loop instead of recursion
D. It does not handle the case when node has no right child

Solution

  1. Step 1: Check handling of right child

    Code correctly finds leftmost node in right subtree if right child exists.
  2. Step 2: Check updating successor when moving right

    In the else branch (n->data >= root->data), it incorrectly sets succ = root before moving right. Successor should NOT be updated when going right, as n is in (or at) the right subtree.
  3. Step 3: Identify error

    The main logical error is incorrectly updating the successor when traversing right.
  4. Final Answer:

    It incorrectly updates successor when moving right -> Option A
  5. Quick Check:

    Successor updates only on left moves [OK]
Hint: Successor updates only when moving left in BST [OK]
Common Mistakes:
  • Thinking recursion is required
  • Updating successor when moving right
  • Ignoring right subtree case
5. You have a BST and want to find the inorder successor of the node with value 25. The BST is:

         20
        /  \
      10    30
     /  \  /  \
    5  15 25  40
If the node with value 25 has no right child, what is the inorder successor and why?
hard
A. 20, because it is the root
B. 40, because it is the right child of 30
C. No successor, because 25 has no right child
D. 30, because it is the lowest ancestor greater than 25

Solution

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

    Node 25 has no right child, so successor is an ancestor.
  2. Step 2: Find lowest ancestor greater than 25

    Ancestor 30 is greater than 25 and is the lowest such ancestor.
  3. Final Answer:

    30, because it is the lowest ancestor greater than 25 -> Option D
  4. Quick Check:

    Successor = nearest bigger ancestor [OK]
Hint: If no right child, successor is nearest bigger ancestor [OK]
Common Mistakes:
  • Choosing right child 40 incorrectly
  • Saying no successor exists
  • Choosing root 20 incorrectly
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