Understanding the Problem
In a Binary Search Tree (BST), the inorder predecessor of a node is the node that comes immediately before it in the in-order traversal (Left → Root → Right).
We are given a node, and our task is to find its inorder predecessor — the previous node visited if we were walking the BST in in-order order.
We’ll now walk through a clear, beginner-friendly approach to solve this, using a step-by-step method with an example and edge case handling.
Step-by-Step Solution with Example
Step 1: Understand the Inorder Traversal
In-order traversal of a BST always produces the nodes in sorted (ascending) order. So, the inorder predecessor of a given node is the node that appears just before it in this sorted order.
Step 2: Case 1 – Node Has a Left Subtree
If the node has a left child, then its inorder predecessor is the rightmost node in that left subtree.
For example, consider the BST below:
6
/ 4 8
5
Let’s say the target node is 6. Since it has a left subtree rooted at 4, we go to 4 and move right until the end. The rightmost node is 5, which is the inorder predecessor.
Step 3: Case 2 – Node Has No Left Subtree
If the node doesn't have a left subtree, we have to look upwards to its ancestors. Specifically, the inorder predecessor is the lowest ancestor of the node, where the node lies in the right subtree.
For example, in this BST:
1
2
3
If our target is node 3, it has no left child. We look at the path from root to 3: 1 → 2 → 3. Among these, 2 is the last node for which 3 was in the right subtree. So, 2 is the inorder predecessor.
Step 4: Case 3 – Node is the Leftmost in the Tree
If the node is the smallest element in the BST (i.e., no node lies before it), then it has no inorder predecessor.
Example:
1
2
The inorder predecessor of 1 is null since there’s no node smaller than it in the tree.
Step 5: Traverse and Track Predecessor While Searching
When searching for the node, we can also track the potential predecessor. Every time we move right in the tree (i.e., node.val < target.val), the current node could be a predecessor.
We keep moving down the tree while tracking this until we find the target node.
Edge Cases
Empty Tree
If the tree is empty, there is no node to search. So the predecessor is null.
Single Node Tree
If the tree has only one node and that is the target, there’s no other node to act as predecessor. So the answer is null.
Example: Tree = [5], target = 5 → Output = null.
Node Not Found
If the target node doesn’t exist in the tree, we return null or handle as per the requirement. Always validate the input.
Multiple Nodes with Same Value
In standard BSTs, duplicates are usually not allowed. But if allowed, clarify whether you want the predecessor of the first occurrence or some specific one.
Finally
To find the inorder predecessor in a BST, you need to carefully consider whether the node has a left child or not. Based on that, either go down to the rightmost node in the left subtree or go up to the closest ancestor where the node lies in the right subtree.
Always keep edge cases in mind like null trees, single node trees, and nodes with no predecessors.
Understanding how inorder traversal works helps build strong intuition for solving many binary search tree problems.