Skip to content

Instantly share code, notes, and snippets.

@Ray1988
Created March 14, 2013 20:14
Show Gist options
  • Save Ray1988/5164814 to your computer and use it in GitHub Desktop.
Save Ray1988/5164814 to your computer and use it in GitHub Desktop.
check a binary tree is a binary search tree
public boolean checkBinarySearchTree(TreeNode r){
if(r==null) return true;
ArrayList<Integer> al=new ArrayList();
Arraylist<Integer> arraylist=inOrderCreateArray(r, al);
return sortedArrayList(arraylist);
}
public ArrayList inOrderCreateArray(TreeNode r, ArrayList<Integer> al){
if (r!=null){
if(r.left!=null)
al=inOderCreateArray(r.left, al);
al.add(r.data);
if(r.right!=null)
al=inOdderCreateArray(r.right, al);
}
return al;
}
public boolean sortedArrayList(ArrayList<Integer> al){
for(int i=0; i<al.size(); i++){
if(al.get(i)>al.get(i+1)){
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment