Bird
Raised Fist0
DSA C++programming

BST Inorder Predecessor in DSA C++

Choose your learning style10 modes available
Mental Model
The inorder predecessor of a node in a BST is the node with the next smaller value, found by going left once and then right as far as possible.
Analogy: Imagine a sorted list of books on a shelf. To find the book just before a given one, you look to the left shelf and then pick the rightmost book there.
      20
     /  \
   10    30
  /  \     
 5   15    
       
↑ current node: 15
Dry Run Walkthrough
Input: BST: 20 -> 10 -> 30 -> 5 -> 15; find inorder predecessor of node 15
Goal: Find the node with the largest value smaller than 15
Step 1: Start at node 15, check if it has left child
20 -> 10 -> 30 -> 5 -> [15↑]
Why: We look left to find smaller values
Step 2: Node 15 has no left child, so move up to parent 10
20 -> [10↑] -> 30 -> 5 -> 15
Why: No left subtree means predecessor is an ancestor smaller than 15
Step 3: Since 15 > 10, 10 is candidate predecessor
20 -> [10↑] -> 30 -> 5 -> 15
Why: We track last node smaller than 15 while moving up
Step 4: Move up to 20, which is greater than 15, stop
[20↑] -> 10 -> 30 -> 5 -> 15
Why: 20 is larger, so predecessor is last smaller node 10
Result:
Inorder predecessor of 15 is 10
Annotated Code
DSA C++
#include <iostream>
using namespace std;

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

Node* insert(Node* root, int val, Node* parent = nullptr) {
    if (!root) {
        Node* n = new Node(val);
        n->parent = parent;
        return n;
    }
    if (val < root->val) {
        root->left = insert(root->left, val, root);
    } else {
        root->right = insert(root->right, val, root);
    }
    return root;
}

Node* maximum(Node* node) {
    while (node->right) {
        node = node->right; // go right as far as possible
    }
    return node;
}

Node* inorderPredecessor(Node* node) {
    if (!node) return nullptr;
    if (node->left) {
        // predecessor is max in left subtree
        return maximum(node->left);
    }
    // else go up until node is right child of parent
    Node* p = node->parent;
    while (p && node == p->left) {
        node = p;
        p = p->parent; // move up
    }
    return p;
}

void printNode(Node* node) {
    if (node) cout << node->val << "\n";
    else cout << "No predecessor\n";
}

int main() {
    Node* root = nullptr;
    root = insert(root, 20);
    root = insert(root, 10);
    root = insert(root, 30);
    root = insert(root, 5);
    root = insert(root, 15);

    // find node 15
    Node* curr = root->left->right; // 15
    Node* pred = inorderPredecessor(curr);
    printNode(pred);
    return 0;
}
while (node->right) { node = node->right; }
go right as far as possible to find max in left subtree
if (node->left) { return maximum(node->left); }
if left subtree exists, predecessor is max node there
while (p && node == p->left) { node = p; p = p->parent; }
move up until node is right child of parent to find predecessor
return p;
return found predecessor or nullptr if none
OutputSuccess
10
Complexity Analysis
Time: O(h) because we traverse up or down the tree height h at most once
Space: O(1) because we use only a few pointers, no extra data structures
vs Alternative: Naive approach would do inorder traversal O(n) to find predecessor, this is faster using BST properties
Edge Cases
Node has no left subtree and is the smallest node
No predecessor found, returns nullptr
DSA C++
while (p && node == p->left) { node = p; p = p->parent; }
Node has left subtree
Predecessor is maximum node in left subtree
DSA C++
if (node->left) { return maximum(node->left); }
Node is null
Returns nullptr safely
DSA C++
if (!node) return nullptr;
When to Use This Pattern
When asked to find the previous smaller element in a BST in sorted order, use the inorder predecessor pattern by checking left subtree or moving up ancestors.
Common Mistakes
Mistake: Trying to find predecessor by always going left without checking rightmost node in left subtree
Fix: Go left once, then go right as far as possible to find maximum in left subtree
Mistake: Not moving up ancestors when no left subtree exists
Fix: Traverse up parents until current node is right child of parent
Summary
Finds the node with the next smaller value than a given node in a BST.
Use when you need the previous element in sorted order in a BST.
If left subtree exists, predecessor is max there; else move up until node is right child.

Practice

(1/5)
1. What is the inorder predecessor of a node in a Binary Search Tree (BST)?
easy
A. The node with the smallest value larger than the given node
B. The node with the largest value smaller than the given node
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 values.
  2. Step 2: Define inorder predecessor

    The inorder predecessor of a node is the node visited just before it in inorder traversal, which means it has the largest value smaller than the current node.
  3. Final Answer:

    The node with the largest value smaller than the given node -> Option B
  4. Quick Check:

    Inorder predecessor = largest smaller value [OK]
Hint: Predecessor is just before in sorted order [OK]
Common Mistakes:
  • Confusing predecessor with successor
  • Thinking predecessor is always left child
  • Assuming predecessor is the parent node
2. Which of the following C++ function signatures correctly represents a function to find the inorder predecessor of a BST node given its pointer Node* root and Node* target?
easy
A. Node* findInorderPredecessor(Node* root, Node* target);
B. void findInorderPredecessor(Node* root, int target);
C. int findInorderPredecessor(Node* root, Node* target);
D. Node findInorderPredecessor(Node root, Node target);

Solution

  1. Step 1: Analyze parameter types

    The function needs the root pointer and the target node pointer to find the predecessor.
  2. Step 2: Analyze return type

    The predecessor is a node pointer, so return type should be Node*.
  3. Final Answer:

    Node* findInorderPredecessor(Node* root, Node* target); -> Option A
  4. Quick Check:

    Correct parameters and return type [OK]
Hint: Return pointer to predecessor node [OK]
Common Mistakes:
  • Using int return type instead of Node*
  • Passing target as int instead of Node*
  • Passing nodes by value instead of pointer
3. Consider the following BST and code snippet to find the inorder predecessor of node with value 15:
struct Node {
  int val;
  Node* left;
  Node* right;
};

Node* findMax(Node* root) {
  while (root->right) root = root->right;
  return root;
}

Node* inorderPredecessor(Node* root, Node* target) {
  if (target->left) return findMax(target->left);
  Node* predecessor = nullptr;
  while (root) {
    if (target->val > root->val) {
      predecessor = root;
      root = root->right;
    } else if (target->val < root->val) {
      root = root->left;
    } else break;
  }
  return predecessor;
}

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

Node* root = ...; // root of above BST
Node* target = root->left->right; // node with value 15
Node* pred = inorderPredecessor(root, target);
std::cout << pred->val << std::endl;
What is the output?
medium
A. 10
B. 15
C. 20
D. 30

Solution

  1. Step 1: Check if target has left subtree

    Node 15 has no left child, so we skip findMax on left subtree.
  2. Step 2: Traverse from root to find predecessor

    Start at root (20). Since 15 < 20, go left to 10. Since 15 > 10, set predecessor = 10 and go right to 15. Found target, stop.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    Largest smaller value than 15 is 10 [OK]
Hint: If no left child, track last smaller ancestor [OK]
Common Mistakes:
  • Returning target node itself
  • Returning root node 20 incorrectly
  • Confusing predecessor with successor
4. The following code attempts to find the inorder predecessor of a node in a BST but has a bug. Identify the bug.
Node* inorderPredecessor(Node* root, Node* target) {
  if (target->left) {
    Node* temp = target->left;
    while (temp->left) temp = temp->left;
    return temp;
  }
  Node* predecessor = nullptr;
  while (root) {
    if (target->val > root->val) {
      predecessor = root;
      root = root->right;
    } else if (target->val < root->val) {
      root = root->left;
    } else break;
  }
  return predecessor;
}
medium
A. The predecessor variable is not initialized
B. The function does not handle the case when target has no left subtree
C. The function returns nullptr always
D. The loop to find max in left subtree incorrectly goes left instead of right

Solution

  1. Step 1: Check left subtree max search

    To find inorder predecessor in left subtree, we must find the maximum node, which is the rightmost node, but code goes left repeatedly.
  2. Step 2: Confirm other parts are correct

    The rest of the code correctly handles cases without left subtree and initializes predecessor.
  3. Final Answer:

    The loop to find max in left subtree incorrectly goes left instead of right -> Option D
  4. Quick Check:

    Max in left subtree requires right traversal [OK]
Hint: Max in left subtree means go right, not left [OK]
Common Mistakes:
  • Going left instead of right to find max
  • Not checking if target->left is null
  • Confusing predecessor with successor
5. Given a BST with nodes inserted in this order: 25, 15, 50, 10, 22, 35, 70, 4, 12, 18, 24, 31, 44, 66, 90. Write a function to find the inorder predecessor of a node with value 31. What will be the predecessor's value? Hint: Use the BST property and consider both cases when the node has a left subtree or not.
hard
A. 25
B. 35
C. 24
D. 22

Solution

  1. Step 1: Locate node 31

    Node 31 is the left child of 35 and has no left subtree.
  2. Step 2: Traverse from root to find predecessor

    Start at root 25 (31 > 25, predecessor=25, go right to 50). 31 < 50, go left to 35. 31 < 35, go left to 31. Found target, predecessor=25.
  3. Step 3: Verify with inorder traversal

    Inorder traversal sequence around 31 is ... 24, 25, 31, 35 ... The predecessor of 31 is the largest smaller value, which is 24 (left subtree of 25).
  4. Final Answer:

    24 -> Option C
  5. Quick Check:

    Inorder predecessor of 31 is 24 [OK]
Hint: If no left child, predecessor is last smaller ancestor or max in left subtree [OK]
Common Mistakes:
  • Choosing 25 instead of 24
  • Choosing successor 35 instead of predecessor
  • Ignoring left subtree check
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