check if a tree is a binary search tree
- by TimeToCodeTheRoad
I have written the following code to check if a tree is a Binary search tree. Please help me check the code:
Pair p{
boolean isTrue;
int min;
int max;
}
public boo lean isBst(BNode v){
return isBST1(v).isTrue;
}
public Pair isBST1(BNode v){
if(v==null)
return new Pair(true, INTEGER.MIN,INTEGER.MAX);
if(v.left==null && v.right==null)
return new Pair(true, v.data, v.data);
Pair pLeft=isBST1(v.left);
Pair pRight=isBST1(v.right);
boolean check=pLeft.max<v.data && v.data<= pRight.min;
Pair p=new Pair();
p.isTrue=check&&pLeft.isTrue&&pRight.isTrue;
p.min=pLeft.min;
p.max=pRight.max;
return p;
}
Note: This function checks if a tree is a binary search tree