[LintCode] Convert Sorted Array to Binary Search Tree With Minimal Height
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public TreeNode sortedArrayToBST(int[] A) { // write your code here return rec(A,0, A.length - 1); } public TreeNode rec(int[] A, int start, int end) { if(start > end) return null; int mid = start + (end - start) / 2; TreeNode root = new TreeNode(A[mid]); root.left = rec(A,start,mid-1); root.right = rec(A,mid+1, end); return root; } |
Leave A Comment