I recently encountered with a question in an interview at e-commerce company. The interviewer asked the following question:
There is BST given with root node with key part as integer only. The structure of each node is as follows:
You need to find the inorder successor and predecessor of a given key. In case the given key is not found in BST, then return the two values within which this key will lie.
Following is the algorithm to reach the desired result. Its a recursive method:
Input: root node, key output: predecessor node, successor node 1. If root is NULL then return 2. if key is found then a. If its left subtree is not null Then predecessor will be the right most child of left subtree or left child itself. b. If its right subtree is not null The successor will be the left most child of right subtree or right child itself. return 3. If key is smaller then root node set the successor as root search recursively into left subtree else set the predecessor as root search recursively into right subtree
Following is the implementation of the above algorithm:
Predecessor is 60 Successor is 70
Another Approach :
We can also find the inorder successor and inorder predecessor using inorder traversal . Check if the current node is smaller than the given key for predecessor and for successor, check if it is greater than the given key . If it is greater than the given key then, check if it is smaller than the already stored value in successor then, update it . At last, get the predecessor and successor stored in q(successor) and p(predecessor).
Thanks Shweta for suggesting this method.
This article is contributed by algoLover. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Inorder predecessor and successor for a given key in BST | Iterative Approach
- Replace each node in binary tree with the sum of its inorder predecessor and successor
- Populate Inorder Successor for all nodes
- Inorder Successor in Binary Search Tree
- Inorder Successor of a node in Binary Tree
- Preorder predecessor of a Node in Binary Tree
- Level Order Predecessor of a node in Binary Tree
- Postorder predecessor of a Node in Binary Search Tree
- Preorder Successor of a Node in Binary Tree
- Postorder successor of a Node in Binary Tree
- Level Order Successor of a node in Binary Tree
- Pre-Order Successor of all nodes in Binary Search Tree
- Inorder traversal of an N-ary Tree
- Preorder from Inorder and Postorder traversals
- Inorder Tree Traversal without Recursion
- Find n-th node of inorder traversal
- Construct Tree from given Inorder and Preorder traversals
- Inorder Tree Traversal without recursion and without stack!
- Find all possible binary trees with given Inorder Traversal
- Construct a Binary Tree from Postorder and Inorder
Improved By : SHUBHAMSINGH10