Search a node in BST
PROBLEM :
Given a Binary Search Tree (BST) your task is to complete the function search which returns true if the node with value x is present in the BST else returns false. You should not read any input from stdin/console. There are multiple test cases. For each test case, this method will be called individually.
Input:
The first line of the input contains an integer 'T' denoting the number of test cases. Then 'T' test cases follow. Each test case consists of three lines. Description of test cases is as follows:
The First line of each test case contains an integer 'N' which denotes the no of nodes to be inserted in the BST. .
The Second line of each test case contains 'N' space separated values denoting the values of the BST. In the third line is an integer x denoting the node to be searched for.
Output:
The output for each test case will be 1 if the node is present in the BST else 0.
Constraints:
1 <= T <= 100
1 <= N <= 100
Example(To be used only for expected output) :
Input
2
7
2 81 87 42 66 90 45
87
4
6 7 9 8
11
Output
1
0
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* Node structure
struct node {
int data;
struct node * right, * left;
};*/
/*You are required to complete this method*/
bool search(node* root, int x)
{
if(root==NULL)
return false ;
if(root->data==x)
return true ;
else if(root->data>x)
return search(root->left,x) ;
else
return search(root->right,x) ;
}
---------------------------------------------------------------------------------
Given a Binary Search Tree (BST) your task is to complete the function search which returns true if the node with value x is present in the BST else returns false. You should not read any input from stdin/console. There are multiple test cases. For each test case, this method will be called individually.
Input:
The first line of the input contains an integer 'T' denoting the number of test cases. Then 'T' test cases follow. Each test case consists of three lines. Description of test cases is as follows:
The First line of each test case contains an integer 'N' which denotes the no of nodes to be inserted in the BST. .
The Second line of each test case contains 'N' space separated values denoting the values of the BST. In the third line is an integer x denoting the node to be searched for.
Output:
The output for each test case will be 1 if the node is present in the BST else 0.
Constraints:
1 <= T <= 100
1 <= N <= 100
Example(To be used only for expected output) :
Input
2
7
2 81 87 42 66 90 45
87
4
6 7 9 8
11
Output
1
0
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* Node structure
struct node {
int data;
struct node * right, * left;
};*/
/*You are required to complete this method*/
bool search(node* root, int x)
{
if(root==NULL)
return false ;
if(root->data==x)
return true ;
else if(root->data>x)
return search(root->left,x) ;
else
return search(root->right,x) ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment