I am trying to check whether a given binary tree is a Binary Search Tree or not. What I am trying to do is perform an inorder traversal of the binary tree and compare the current element with the previous element. In case the current element is greater we go on and check further else the given tree is invalid.
int prev=0;
public int isValidBST(TreeNode A) {
if(A==null)
return 1;
isValidBST(A.left);
// System.out.println("val "+A.val);
if(A.val<=prev)
return 0;
// else if(A!=null){
prev=A.val;
//System.out.println("prev "+prev);
isValidBST(A.right);
if(isValidBST(A.right)==1&&isValidBST(A.left)==1)
return 1;
return 0;
}
So this is the code that I have written. What I am doing wrong here?