> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Convert Sorted Array to Binary Search Tree

> Tested Python solution for LeetCode 108 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 108, Easy. Topics: Array, Divide and Conquer, Tree, Binary Search Tree, Binary Tree. [View on LeetCode](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 108   # by problem number
lcpy gen -s convert_sorted_array_to_binary_search_tree   # by problem name
```

## Problem

Given an integer array `nums` where the elements are sorted in ascending order, convert it to a **height-balanced** binary search tree.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg)

```
Input: nums = [-10,-3,0,5,9]
Output: [0,-3,9,-10,null,5]
Explanation: [0,-10,5,null,-3,null,9] is also accepted:

![Alt](https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg)
```

![Example 2](https://assets.leetcode.com/uploads/2021/02/18/btree.jpg)

```
Input: nums = [1,3]
Output: [3,1]
Explanation: [1,null,3] and [3,1] are both height-balanced BSTs.
```

### Constraints

* 1 \<= nums.length \<= 10^4
* -10^4 \<= nums\[i] \<= 10^4
* nums is sorted in a strictly increasing order.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/convert_sorted_array_to_binary_search_tree/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/convert_sorted_array_to_binary_search_tree/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import TreeNode


class Solution:
    # Time: O(n) — each element becomes one node
    # Space: O(log n) recursion stack for a balanced build
    def sorted_array_to_bst(self, nums: list[int]) -> TreeNode[int] | None:
        def build(left: int, right: int) -> TreeNode[int] | None:
            if left > right:
                return None
            mid = (left + right) // 2
            node = TreeNode[int](nums[mid])
            node.left = build(left, mid - 1)
            node.right = build(mid + 1, right)
            return node

        return build(0, len(nums) - 1)
```

## Complexity

| Time                                 | Space                                         |
| ------------------------------------ | --------------------------------------------- |
| O(n) — each element becomes one node | O(log n) recursion stack for a balanced build |

## Tags

[Grind](/catalog/grind), [NeetCode All](/catalog/neetcode).
