[LintCode] Convert Sorted Array to Binary Search Tree With Minimal Height
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