177.Convert Sorted Array to Binary Search Tree With Minimal Height
Last updated
Last updated
public TreeNode sortedArrayToBST(int[] A) {
return dfs(A,0,A.length-1);
}
public TreeNode dfs(int[] A,int left,int right){
if(left>right){
return null;
}
int mid=(left+right)/2;
TreeNode root=new TreeNode(A[mid]);
root.left=dfs(A,left,mid-1);
root.right=dfs(A,mid+1,right);
return root;
}