CHECK FOR BST(Is this approach is right?)

Can we solve this problem by using approach:
Compute the inorder then check if the inorder is sorted then it is BST otherwise it is not BST.

hey Ambuj!, yes this can also be a valid approach but can be violated on edge case where there are duplicates in BST, as by definition, right_val>=parent_val and left_val<parent_val, you can violate this condition while inorder is still sorted!


see that tree in black is not BST while tree in red is although both have same inorder traversal.

But I have seen on many sites like GFG that in BST duplicate nodes are not allowed.Is it right?

Well this depends!, in simple words, your approach is correct only if duplicates are not allowed.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.