Is This a Binary Search Tree?

  • + 2 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;
            
       }