Skip to content

Instantly share code, notes, and snippets.

@iwilbert
Created July 13, 2014 21:39
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 iwilbert/3b0c485726cd15632bef to your computer and use it in GitHub Desktop.
Save iwilbert/3b0c485726cd15632bef to your computer and use it in GitHub Desktop.
public TreeNode buildTree(int[] A) {
return buildBST(A, 0, A.length-1);
}
private TreeNode buildBST(int[] A, int left, int right) {
TreeNode root = null;
if (left <= right) {
int mid = left + (right - left) / 2;
root = new TreeNode(A[mid]);
root.left = buildBST(A, left, mid-1);
root.right = buildBST(A, mid+1, right);
}
return root;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment