We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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;
}
Cookie support is required to access HackerRank
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 →