Is This a Binary Search Tree?

  • + 0 comments
    Initially, I was failing some test cases but then i changed the condition in inorder function to check for >= instead of > and then it worked. This means that if a node has a parent or a child equal to itself then it voilates the BST property. 
    
    bool inorder(Node* root,vector<int>& vec){
        if(!root){
            return true;
        }
        bool l=inorder(root->left,vec);
        if(vec.size() && vec.back()>=root->data){
            return false;
        }
        vec.push_back(root->data);
        bool r=inorder(root->right,vec);
    
        return l && r;
    
    }
    bool checkBST(Node* root) {
        if(!root){
            return false;
        }
        vector<int> vec;
        if(inorder(root,vec)){
            return true;
        }
    
        return false;
    }