In-order traversal of BST is in ascending order. I’m using this fact to check for valid BST. If minNum
which is the previous node data is greater than the curr node it means it’s not the valid BST. I’m not sure what I’m doing wrong. I recursively go to left node , then check above said condition then go to right node and repeat this to traverse the whole tree.
Please tell me what I’m doing wrong and I don’t want any other method of solving this problem.
Thanks
bool flag = true;
bool validateBSTHelperFunc(Node *root, int minNum)
{
if (root == nullptr)
{
return true;
}
validateBSTHelperFunc(root->left, minNum);
if (root->data <= minNum)
{
return false;
}
minNum = root->data;
validateBSTHelperFunc(root->right, minNum);
}
bool validateBST(Node *root)
{
int minNum = INT_MIN;
flag = validateBSTHelperFunc(root, minNum);
return flag;
}