> ## 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 List to Binary Search Tree

> Tested Python solution for LeetCode 109 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 109, [Medium](/catalog/medium). Topics: [Linked List](/catalog/topics/linked-list), [Divide and Conquer](/catalog/topics/divide-and-conquer), [Tree](/catalog/topics/tree), [Binary Search Tree](/catalog/topics/binary-search-tree), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/).

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

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

## Problem

Given the `head` of a singly linked list where elements are sorted in **ascending order**, convert it to a **height-balanced** binary search tree.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/08/17/linked.jpg)

```
Input: head = [-10,-3,0,5,9]
Output: [0,-3,9,-10,null,5]
Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.
```

```
Input: head = []
Output: []
```

### Constraints

* The number of nodes in head is in the range \[0, 2 \* 10^4].
* -10^5 \<= Node.val \<= 10^5

## Solution

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

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


class Solution:
    # Time: O(n) - each list node is visited once, in the same order the
    # in-order traversal consumes them.
    # Space: O(log n) - recursion depth equals the tree height.
    def sorted_list_to_bst(self, head: ListNode[int] | None) -> TreeNode[int] | None:
        size = 0
        node = head
        while node is not None:
            size += 1
            node = node.next

        cursor = head

        def build(lo: int, hi: int) -> TreeNode[int] | None:
            nonlocal cursor
            if lo > hi:
                return None
            mid = (lo + hi) // 2
            left = build(lo, mid - 1)
            cur = cursor
            assert cur is not None
            cursor = cur.next
            return TreeNode(cur.val, left, build(mid + 1, hi))

        return build(0, size - 1)
```

## Complexity

| Time | Space |
| - | - |
| O(n) - each list node is visited once, in the same order the | O(log n) - recursion depth equals the tree height. |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.