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

> Tested Python solution for LeetCode 426 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 426, [Medium](/catalog/medium). Topics: [Stack](/catalog/topics/stack), [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Binary Search Tree](/catalog/topics/binary-search-tree), [Linked List](/catalog/topics/linked-list), [Binary Tree](/catalog/topics/binary-tree), Doubly-Linked List. [View on LeetCode](https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/description/).

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

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

## Problem

Convert a **Binary Search Tree** to a sorted **Circular Doubly-Linked List** in place.

You can think of the left and right pointers as synonymous to the predecessor and successor pointers in a doubly-linked list. For a circular doubly linked list, the predecessor of the first element is the last element, and the successor of the last element is the first element.

We want to do the transformation **in place**. After the transformation, the left pointer of the tree node should point to its predecessor, and the right pointer should point to its successor. You should return the pointer to the smallest element of the linked list.

### Examples

![Example 1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0426.Convert%20Binary%20Search%20Tree%20to%20Sorted%20Doubly%20Linked%20List/images/bstdlloriginalbst.png)

```
Input: root = [4,2,5,1,3]
Output: [1,2,3,4,5]

Explanation: The figure below shows the transformed BST. The solid line indicates the successor relationship, while the dashed line means the predecessor relationship.
```

```
Input: root = [2,1,3]
Output: [1,2,3]
```

### Constraints

* The number of nodes in the tree is in the range `[0, 2000]`.
* `-1000 <= Node.val <= 1000`
* All the values of the tree are **unique**.

## Solution

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

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


class Solution:
    # Time: O(n)
    # Space: O(h) recursion stack
    def tree_to_doubly_list(self, root: TreeNode[int] | None) -> TreeNode[int] | None:
        if root is None:
            return None
        first: TreeNode[int] | None = None
        last: TreeNode[int] | None = None

        def link(node: TreeNode[int]) -> None:
            nonlocal first, last
            if last is not None:
                last.right = node
                node.left = last
            else:
                first = node
            last = node

        def dfs(node: TreeNode[int] | None) -> None:
            if node is None:
                return
            dfs(node.left)
            link(node)
            dfs(node.right)

        dfs(root)
        assert last is not None and first is not None
        last.right = first
        first.left = last
        return first
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(h) recursion stack |

## Tags

[NeetCode All](/catalog/neetcode).


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