Difficulty: Easy, Asked-in: Amazon, Microsoft, Ola.
Key takeaway: An excellent problem to learn problem-solving using both iterative and recursive approaches in the binary search tree.
Let’s understand the problem
Write a program to find the in-order predecessor of a given node x in a binary search tree (BST). An in-order predecessor of node x is the node that will be visited just before node x in the in-order traversal.
Problem note: If node x is visited first (leftmost node in BST), then the predecessor of node x is NULL. In other words, the in-order predecessor of the node with the minimum value does not exist in the BST.
Iterative solution using parent pointer
Solution idea
If the left subtree of node x is present, the in-order predecessor of node x is a node with the maximum value in its left subtree. If the left subtree of node x doesn't exist, then there are two cases:
- If node x is the right child of its parent, then in-order predecessor will be its parent node.
- If node x is the left child of its parent, the in-order predecessor will be one of the ancestors. To find such an ancestor, we traverse up the tree towards the root until we encounter a node y that is the right child of its parent. If such a node y is present, then the in-order predecessor of x is the parent of node y.
The critical question is: How can we move up in the BST? To implement this, we include a parent pointer in the BST node structure, which points to the parent of the given node.
Solution steps
- If (x->left != NULL), we find the maximum node in the left subtree and return it as the predecessor, i.e., return bstMaximum(x->left). Otherwise, if(x->parent != NULL && x->parent->right == x), we return x->parent as the predecessor.
- Otherwise, we need to find the first ancestor of node x which is the right child of its parent. If such a node is present, then the in-order predecessor is its parent. To find this:
- We start by initialising a pointer curr with node x and a pointer p to the parent of node x.
- We now run a loop as long as p is not NULL and curr is the left child of its parent (p->left == curr).
- Within the loop, we continue moving up by updating curr to p and p to its parent (p = p->parent).
- At the end of the loop, curr will be the first ancestor of node x which is the right child of its parent, and p will be its parent. So, we return pointer p as the predecessor.
Solution pseudocode
bstMaximum(root)
while root.right is not null
root = root.right
return root
inorderPredecessor(x)
if x is null
return null
if x.left is not null
return bstMaximum(x.left)
if x.parent is not null and x.parent.right == x
return x.parent
p = x.parent
curr = x
while p is not null and p.left == curr
curr = p
p = p.parent
return pTime and space complexity analysis
If the left child of node x is present, then the maximum value in the left subtree will be the predecessor. In the worst case, node x will be the root node and the node with max value in the left subtree is the deepest leaf node. Here we need to traverse the height of the tree. So time complexity in the worst case = O(h).
If the left child of x is not present, then we traverse up to find the predecessor. In the worst case, x will be one of the deepest leaf nodes, and the root node will be the predecessor. Here, we need to traverse the complete height of the tree. So the time complexity in the worst case = O(h).
Overall, the time complexity will be O(h), which depends on the height of the tree. Since we are storing an extra parent pointer inside each node, the space complexity = O(n).
Iterative solution without using parent pointer
Storing a parent pointer inside the BST node helps us easily traverse up the tree, but it requires O(n) extra space. Now, the critical question is: Can we solve this problem without using a parent pointer? Let’s think!
The idea is: We maintain a pred pointer to track the predecessor and move down from the root to node x by comparing the root->key with the x->key.
- If x->key < root->key, we move left. There is no need to update the pred pointer because the current node root will not be the candidate for the predecessor. The idea is simple: the value of the pred->key will always be less than the x->key.
- If x->key > root->key, we update the pred pointer to the current node root and move to the right subtree to look for a closer candidate that is still smaller than x. We update the pred pointer because the current node root will be the candidate for the predecessor. Think!
- If x->key == root->key, we have found the node. So we check if the left subtree of node x exists. If it does, we update the pred pointer to the maximum node in the left subtree (pred = bstMaximum(x->left)) and break from the loop.
By the end of this loop, we return the pred pointer.
Solution pseudocode
bstMaximum(root)
while root.right is not null
root = root.right
return root
inorderPredecessor(root, x)
if root is null
return null
pred = null
while root is not null
if x.key < root.key
root = root.left
else if x.key > root.key
pred = root
root = root.right
else
if root.left is not null
pred = bstMaximum(root.left)
break
return predTime and space complexity analysis
During the search process, we need to traverse the entire height of the tree in the worst case. So the time complexity in the worst case = O(h). Since there is no parent pointer and we use a constant amount of extra space, the space complexity = O(1).
Recursive solution without using parent pointer
In the above solution, we iteratively move down the tree to search for node x and update the predecessor. The critical question is: Can we think of implementing it using recursion?
Solution idea
The idea is to recursively move down to search for node x in the BST and keep updating the predecessor. When we find the node x and the left subtree of node x is present, we return the node with the maximum value in the left subtree of x. If the left subtree of node x doesn't exist, then the in-order predecessor is one of its ancestors, which has already been updated while searching.
- If root->key == x->key, we check if node x has a left subtree. If it does, we return the maximum node in the left subtree as the predecessor, i.e., return bstMaximum(root->left).
- If root->key > x->key, we recursively call the same function on the left subtree to continue searching for the predecessor in the left subtree, i.e., inorderPredecessor(root->left, pred, x). There is no need to update the pred pointer because pred->key will always be less than the x->key.
- If root->key < x->key, we update the pred pointer to the current node root and recursively call the same function on the right subtree, i.e., inorderPredecessor(root->right, pred, x).
- Finally, we return the pred pointer.
Solution pseudocode
bstMaximum(root)
while root.right is not null
root = root.right
return root
inorderPredecessor(root, pred, x)
if root is null
return pred
if root.key == x.key
if root.left is not null
return bstMaximum(root.left)
return pred
else if root.key > x.key
return inorderPredecessor(root.left, pred, x)
else
pred = root
return inorderPredecessor(root.right, pred, x)Time and space complexity analysis
At each step of recursion, we are going one level down. So in the worst case, we will be traversing the complete height of the tree. So time complexity = O(h). Space complexity will depend on the size of the call stack, which will be equal to the height of the recursion tree. So space complexity = O(h).
Using inorder traversal of the binary search tree
Solution idea
As we know, the in-order predecessor of node x is the node that will be visited just before node x in the in-order traversal. The critical question is: Can we solve it using an in-order traversal? Here is an idea!
We traverse the BST in in-order manner and keep track of the previous node visited. When we find the target node, the previous node represents its in-order predecessor. To ensure that the changes to the previous node are propagated correctly across the recursive calls, we use a pointer prev and pass it as a reference during the recursive call.
Solution pseudocode
inorder(root, x, prev)
if root is null
return null
leftResult = inorder(root.left, x, prev)
if leftResult is not null
return leftResult
if root.key == x.key
return prev
prev = root
return inorder(root.right, x, prev)
inorderPredecessor(root, x)
if root is null or x is null
return null
prev = null
return inorder(root, x, prev)Time and space complexity analysis
In the worst case, we will traverse each node once using in-order traversal. So time complexity will be O(n) in the worst case. Inside the code, we use constant extra space, but recursion will use a call stack with a size equal to the height of the tree. So space complexity is O(h).
Critical idea to think!
- How can we solve this problem using iterative in-order traversal?
- How can we modify the above code to find both the in-order successor and predecessor together?
- In the first approach using a parent pointer, is there a need to handle this condition: if (x->parent != NULL && x->parent->right == x)?
- In the second and third approaches, why do we update the pred pointer when we move to the right subtree?
- Which method is better in terms of time and space complexity?
- How can we solve this problem by storing the in-order traversal of the BST in an array?
- The time complexity of all the above approaches is O(h). What would be the worst and best-case scenarios?
- Can we consider implementing the above code in a different manner?
Suggested coding questions to practice
- Replace each node in the binary tree with the sum of its inorder predecessor and successor
- Populate Inorder predecessor for all nodes
- Find the in-order successor of a node in BST
- In-order predecessor of a node in binary tree
- The lowest common ancestor of a binary search tree
- Validate binary search tree
- The kth largest element in a BST
- Convert sorted array to binary search tree
If you have any queries or feedback, please write us at contact@enjoyalgorithms.com. Enjoy learning, Enjoy algorithms!
