QUES on bst
Wed Nov 06 2024 20:18:52 GMT+0000 (Coordinated Universal Time)
Saved by
@E23CSEU1151
///************** BST QUESTION*************/////////////
/// IN THIS QUESTION THEY WILL GIVE US A NUMBER AND IF IT IS PRESENT IN THAT TREE GIVE US TRUE OTHERWISE RETURN FALSE TO JUS
/// APPROACH
// if we get null return false otherwise if root->data > (number passed) go to the left side else go to right side
///// FIRST APPROACH
bool searchInBST(BinaryTreeNode<int> *root, int x){
BinaryTreeNode<int> *temp = root;
while(temp != NULL)
if(temp->data == x){
return true;
}
if(temp->data > x){
temp = temp->left;
}
else{
temp = temp->right;
}
return false;
}
////// second approach
bool searchInBST(BinaryTreeNode<int> *root , int x ){
// base case
if(root == Null){
return false;
}
if(root ->data == x){
return true;
}
if(root->data > x){
return seachInBST(root->left , x );
}
else{
return seachInBST(root->right, x );
}
content_copyCOPY
Comments