# 108. Convert Sorted Array to Binary Search Tree

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-MgYmBCw9LDpqMIMrf9U%2F-Mg_7aHlObQBFRsfkz-u%2Fimage.png?alt=media\&token=6b91e194-1c28-476f-8e3d-f44c5dbd55aa)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-MgYmBCw9LDpqMIMrf9U%2F-Mg_7e3RqzsjjHS1xUH5%2Fimage.png?alt=media\&token=b9061390-04f3-4bb5-8be4-9fa12fc80035)

time: O(n), must visit all nums element

space: O(n), must build all nums element, O(logn) for recursion stack, because use binary search idea

```java
class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        
        return helper(nums, 0, nums.length - 1);
    }
    private TreeNode helper(int[] nums, int left, int right) {
        if (left > right) return null;
        int mid = left + (right - left)/2;
        TreeNode node = new TreeNode(nums[mid]);

        node.left = helper(nums, left, mid - 1);
        node.right = helper(nums, mid + 1, right);
        return node;
    }
}
```
