Java 8 ( Array Supported ) Solution

amazon
Tags: #<Tag:0x00007f2428be5908>

#1
public class Solution {
    // DO NOT MODIFY THE ARGUMENTS WITH "final" PREFIX. IT IS READ ONLY
    public TreeNode sortedArrayToBST(final int[] A){
        TreeNode root = new TreeNode(-1);
        return solve(root, A, 0, A.length - 1);
    }
    private TreeNode solve(TreeNode root, int[] arr, int low, int high){
        if(low > high){
            return null;
        }
        int mid = low + (high - low)/2;
        root = new TreeNode(arr[mid]);
        
        root.left = solve(root, arr, low, mid-1);
        root.right = solve(root, arr, mid+1, high);
        
        return root;
    }
    
}