Created
March 14, 2013 20:14
-
-
Save Ray1988/5164814 to your computer and use it in GitHub Desktop.
check a binary tree is a binary search tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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