Skip to content

Instantly share code, notes, and snippets.

@charlespunk
Last active December 12, 2015 03:29
Show Gist options
  • Save charlespunk/4707635 to your computer and use it in GitHub Desktop.
Save charlespunk/4707635 to your computer and use it in GitHub Desktop.
Given a sorted(increasing order) array, write an algorithm to create a binary search tree with minimal height.
public static Node creatBST(int[] array){
return division(array, 0, array.length - 1);
}
public static Node division(int[] array, int begin, int end){
if(begin < end) return null;
int mid = (begin + end) / 2;
Node thisNode = new Node(array[mid]);
thisNode.left = division(array, begin, mid - 1);
thisNode.right = division(array, mid + 1, end);
return thisNode;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment