Skip to content

Instantly share code, notes, and snippets.

@jingz8804
Created July 11, 2014 19:50
Show Gist options
  • Save jingz8804/81e1e22299900497f3af to your computer and use it in GitHub Desktop.
Save jingz8804/81e1e22299900497f3af to your computer and use it in GitHub Desktop.
#ctci
public class BSTWithMinHeight{
private class Node{
int val;
Node left;
Node right;
public Node(int val){
this.val = val;
}
}
public Node createBST(int[] data){
if(data == null) return null;
return createBST(data, 0, data.length - 1);
}
private Node createBST(int[] data, int low, int high){
if(low > high) return null;
if(low == high) return new Node(data[low]);
int mid = (low + high)/2;
Node root = new Node(data[mid]);
root.left = createBST(data, low, mid-1);
root.right = createBST(data, mid+1, high);
return root;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment