Predecessor and Successor | Practice | GeeksforGeeks

There is BST given with root node with key part as integer only. You need to find the inorder successor and predecessor of a given key. In case, if the either of predecessor or successor is not found print -1.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains n denoting the number of edges of the BST. The next line contains the edges of the BST. The last line contains the key.
Output:
Print the predecessor followed by successor for the given key. If the predecessor or successor is not found print -1.
Note : You don't need to print anything. You only need to set p.pre to the predecessor and s.succ to the successor. p and s have been passed in the function parameter.
Constraints:
1<=T<=100
1<=n<=100
1<=data of node<=100
1<=key<=100
Example:
Input:
2
6
50 30 L 30 20 L 30 40 R 50 70 R 70 60 L 70 80 R
65
6
50 30 L 30 20 L 30 40 R 50 70 R 70 60 L 70 80 R
100
Output:
60 70
80 -1
 
0
maareftam
This comment was deleted.
-1
neerajchatterjee23012 weeks ago
void inorder(Node* root, vector<Node*> &ans, vector<int> &store){
   if(root == NULL) return;
   
   inorder(root->left, ans, store);
   
   ans.push_back(root);
   store.push_back(root->key);
   
   inorder(root->right, ans, store);
}
// This function finds predecessor and successor of key in BST.
// It sets pre and suc as predecessor and successor respectively
void findPreSuc(Node* root, Node*& pre, Node*& suc, int key)
{
   
   vector<Node*> ans;
   vector<int> store;
   
   inorder(root, ans, store);
   
   // successor
   int pos1 = upper_bound(store.begin(), store.end(), key) - store.begin();
   // predecessor
   int pos2 = lower_bound(store.begin(), store.end(), key) - store.begin();
   
   int n = store.size();
   
   if(pos1 == n){
       suc = NULL;
       if(pos2 >= 1){
           pre = ans[pos2-1];
       }
       else{
           pre = NULL;
       }
   }
   else{
       suc = ans[pos1];
       
       if(pos2 >= 1){
           pre = ans[pos2-1];
       }
       else{
           pre = NULL;
       }
   }
   
   
}
0
sahuprashant291 month ago
Recursive and easy- only 3conditions used:
public static void findPreSuc(Node root, Res p, Res s, int key)
    {
        if(root==null)return;
        
        if(key>root.data){
            p.pre = root;
            findPreSuc(root.right,p,s,key);
        }
        else if(key<root.data){
            s.succ = root;
            findPreSuc(root.left,p,s,key);
        }
        else{
            findPreSuc(root.left,p,s,key);
            findPreSuc(root.right,p,s,key);
        }
    }
0
sahuprashant29
This comment was deleted.
+1
rajatgupta96961 month ago
Node * Success(Node * root){   Node * curr = root->right;   while(curr->left!=NULL)       curr=curr->left;   return curr;}Node * Predecer(Node * root){   Node * curr = root->left;   while(curr->right!=NULL)       curr=curr->right;   return curr;}// This function finds predecessor and successor of key in BST.// It sets pre and suc as predecessor and successor respectivelyvoid findPreSuc(Node* root, Node*& pre, Node*& suc, int key){   if(root==NULL)       return;   if(root->key == key)   {   // suscessor - one right ---> extreme left       if(root->right)            suc=Success(root);   // predessor - one left ---> extreme right       if(root->left)            pre=Predecer(root);       return;   }   if(root->key < key)   {       pre =root;       findPreSuc(root->right,pre,suc,key);   }   else if(root->key > key)   {       suc =root;       findPreSuc(root->left,pre,suc,key);   }
}
+1
ruchitchudasama1231 month ago
void findPreSuc(Node* root, Node*& pre, Node*& suc, int key)
{

   if (!root) return;
    
    // suc
    Node* cur=root;
    while(cur!=NULL)
        if(cur->key > key){
            suc=cur;
            cur=cur->left;
        }
        else cur=cur->right;
    
    // pre
    cur=root;
    while(cur)
        if(cur->key < key){
            pre=cur;
            cur=cur->right;
            
        }else cur=cur->left;
}
+2
koushalsharma0032 months ago
Simple C++ solution. Note that we don't have to consider the node whose value is equal to the key.
void findPreSuc(Node* root, Node*& pre, Node*& suc, int key)
{
// Your code goes here
    if(!root)   return;
    Node* curr = root;
    while(curr)
    {
        if(curr -> key < key)
        {
            pre = curr;
            curr = curr -> right;
        }
        else
        {
            curr = curr -> left;
        }
    }
    
    curr = root;
    while(curr)
    {
        if(curr -> key > key)
        {
            suc = curr;
            curr = curr -> left;
        }
        else
        {
            curr = curr -> right;
        }
    }

}
+1
gowthamreddyuppunuri2 months ago
void findPre(Node *root,Node *&pre,int key){
    if(root == nullptr){
        return;
    }
    if(root->key < key){
        pre = root;
        //just smaller than key
        findPre(root->right,pre,key);
    }
    else if(root->key >= key){
        findPre(root->left,pre,key);
    }
}
void findSuc(Node *root,Node *&suc,int key){
    if(root == nullptr){
        return;
    }
    if(root->key <= key){
        findSuc(root->right,suc,key);
    }
    else if(root->key > key){
        //just greater find in left of root
        suc = root;
        findSuc(root->left,suc,key);
    }
}

void findPreSuc(Node* root, Node*& pre, Node*& suc, int key)
{

// Your code goes here
    findPre(root,pre,key);
    findSuc(root,suc,key);

}
+2
sanketbhagat2 months ago
SIMPLE JAVA SOLUTION
class GfG{
    public static void findPreSuc(Node root, Res p, Res s, int key){
       // add your code here
       
       Node curr = root;
       while(curr!=null){
           if(curr.data<key){
               p.pre = curr;
               curr = curr.right;
           }else curr = curr.left;
       }
       curr = root;
       while(curr!=null){
           if(curr.data>key){
               s.succ = curr;
               curr = curr.left;
           }else curr = curr.right;
       }
    }
}
+3
animesh18362 months ago
void findPreSuc(Node* root, Node*& pre, Node*& suc, int key)
{

    if(not root)return;
    findPreSuc(root->left,pre,suc,key);
    if(root->key < key)pre=root;
    if(root->key>key and not suc)suc=root;
    
    findPreSuc(root->right,pre,suc,key);

}
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.