Search a node in BST | Practice | GeeksforGeeks

Given a Binary Search Tree and a node value X, find if the node with value X is present in the BST or not.

Example 1:
Input:         2
                \
                 81 
               /    \ 
             42      87 
              \       \ 
               66      90 
              / 
            45
X = 87
Output: 1
Explanation: As 87 is present in the
given nodes , so the output will be
1.
Example 2:
Input:      6
             \ 
              8 
             / \ 
            7   9
X = 11
Output: 0
Explanation: As 11 is not present in 
the given nodes , so the output will
be 0.

Your Task:
You don't need to read input or print anything. Complete the function search()which returns true if the node with value x is present in the BSTelse returns false.

Expected Time Complexity: O(Height of the BST)
Expected Auxiliary Space: O(1).

Constraints:
1 <= Number of nodes <= 105
0
shanu146 days ago
C++ Solution :-
bool search(Node* root, int x) {
    if(root==NULL) return 0;
    
    else if(root->data==x) return 1;
    
    else if(x>root->data) search(root->right,x);
    
    else search(root->left,x);
}
-1
sibajit1176be201 week ago
 static int search(Node root,int key) {   Node temp=root;  if (temp==null) {   return -1;  }  if(root.data == key) return 1;  if (root.data<key ){  return search(root.right,key);    }  else if(root.data>key ) {   return search(root.left,key);     }    return -1;        }
0
sarthakacharya3 weeks ago
class BST:
    
    #Function to search a node in BST.
    def search(self, node, x):
        if node is None:
            return 0
        if node.data == x:
            return node
        
        if node.data < x:
            return self.search(node.right,x)
        else:
            return self.search(node.left,x)
0
amishasahu3283 weeks ago
bool search(Node* root, int x) {
    // Your code here
        if(root == NULL) return root;
        if(root->data == x) return root;
        if(root->data > x)
            return search(root->left, x);
        if(root->data < x)
            return search(root->right, x);
        return NULL;
}
0
jaydhurat3 weeks ago
class BST {
   // Function to search a node in BST.
   boolean search(Node root, int x){
       if(root==null)
       {
           return false;
       }
       if(root.data==x)
       {
           return true;
       }
       if(root.data>x)
       {
       return search(root.left,x);   
       }
       
       return search(root.right,x);
   }
   }
0
pyushkumar9993 weeks ago
bool search(Node* root, int x) {
    if(root == NULL) return false;
    if(root->data == x) return true;
    if(root->data < x) return search(root->right,x);
    if(root->data > x) return search(root->left,x);
}
Simple C++ solution using recursion.
0
kritikasinha2564 weeks ago
PYTHON SOLUTION
class BST:   def search(self, node, x):       if node is None:           return None       if x > node.data:           return self.search(node.right, x)       elif x< node.data:           return self.search(node.left, x)       else:           return True       return False 
0
pritamtech19984 weeks ago
One line code: xD
 
bool search(Node* root, int x) {   return (root)?(root->data == x)?true:(root->data < x)?search(root->right, x):search(root->left,x):false;}
+1
19003001001681 month ago
bool flag=false;
void find(Node* root, int x){   if(root==NULL){      return;    }      if(root->data==x){       flag=true;       return;   }      else if(root->data>x){       return find(root->left,x);   }   else if(root->data<x){       return find(root->right,x);   }}
bool search(Node* root, int x) {   // Your code here   find(root,x);   return flag;} 
 
what is wrong in my code???
+1
wilkygandyj1 month ago
//Simple Java Solution
class BST {   // Function to search a node in BST.   boolean search(Node root, int x) {       // Your code here          Node currentNode = root;       while(currentNode != null){          if(currentNode.data == x){              return true;          } else if (currentNode.data < x) {              currentNode = currentNode.right;          } else {             currentNode = currentNode.left;           }          }       return false;   }}
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.