consider a binary tree in c with structure given below
struct node{
int data;
struct node* left;
struct node* right;
};
And this is the code for to check if a given tree is Binary Search Tree or not
int isBST(struct node* root){
static struct node *prev = NULL;
if(root!=NULL){
if(!isBST(root->left)){
return 0;
}
if(prev!=NULL && root->data <= prev->data){
return 0;
}
prev = root;
return isBST(root->right);
}
else{
return 1;
}
}
I want to know why we made the node prev static and when does this part of the code section violate the criteria for BST
if(!isBST(root->left)){
return 0;
}