Created
July 13, 2014 21:39
-
-
Save iwilbert/3b0c485726cd15632bef to your computer and use it in GitHub Desktop.
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 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