You are viewing a single comment's thread. Return to all comments →
Hey ,Even I tried the recursive method but all the test cases didnt pass, Please check the code
bool checkBST(Node* root) { struct Node *tmp=(struct Node*)malloc(sizeof(struct Node)); tmp=root; if(root==NULL) return true; else if(tmp->left!=NULL && tmp->right!=NULL) { if(tmp->data > (tmp->left)->data && tmp->data< (tmp->right)->data) { bool ans1=checkBST(tmp->left); bool ans2=checkBST(tmp->right); if(ans1 && ans2) return true; else return false; } } else if(tmp->left==NULL && tmp->right!=NULL) { if(tmp->data < (tmp->right->data)) return checkBST(tmp->right); else return false; } else if(tmp->left!=NULL && tmp->right==NULL) { if(tmp->data > (tmp->left->data)) return checkBST(tmp->left); else return false; } return true; }
Seems like cookies are disabled on this browser, please enable them to open this website
Is This a Binary Search Tree?
You are viewing a single comment's thread. Return to all comments →
Hey ,Even I tried the recursive method but all the test cases didnt pass, Please check the code