Skip to content

Instantly share code, notes, and snippets.

@jyhjuzi
Created July 15, 2014 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jyhjuzi/8af477cdc9f92a57d842 to your computer and use it in GitHub Desktop.
Save jyhjuzi/8af477cdc9f92a57d842 to your computer and use it in GitHub Desktop.
public class Q4_3{
public static void main(String[] args){
int[] test ={1,2,3,4,5,6,7,8};
TreeNode result = arrayToBST(test,1,6);
printOut(result);
}
private static void printOut(TreeNode result) {
if(result == null)
return;
printOut(result.left);
System.out.println(result.value);
printOut(result.right);
}
static TreeNode<Integer> arrayToBST(int[] array, int start, int end){
if(end== start){
return new TreeNode<Integer>(array[end]);
}
if(end<0||start>array.length||start>end)
return null;
int mid = (start+end)/2;
TreeNode root = new TreeNode<Integer>(array[mid]);
root.left= arrayToBST(array,start,mid-1);
root.right = arrayToBST(array,mid+1,end);
return root;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment