# LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List

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.

**Example 1:**

![](https://assets.leetcode.com/uploads/2018/10/12/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.

```

**Example 2:**

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

**Example 3:**

```
Input: root = []
Output: []
Explanation: Input is an empty tree. Output is also an empty Linked List.
```

**Example 4:**

```
Input: root = [1]
Output: [1]
```

**Constraints:**

* `-1000 <= Node.val <= 1000`
* `Node.left.val < Node.val < Node.right.val`
* All values of `Node.val` are unique.
* `0 <= Number of Nodes <= 2000`

## Solution

[English Version in Youtube](https://youtu.be/SLT9NasJo4U)

[中文版解答Youtube Link](https://youtu.be/-TSxiyrOwIQ)

[中文版解答Bilibili Link](https://www.bilibili.com/video/BV1Cf4y147qd/)

```
class Solution {
    
    void inorder(Node* cur, Node*& prev, Node*& head) {
        if (head == nullptr && cur->left == nullptr) {
            head = cur;
        }
        Node* left = cur->left;
        Node* right = cur->right;

        if (left != nullptr) {
            inorder(left, prev, head);
        }

        if (prev != nullptr) {
            prev->right = cur;
        }
        cur->left = prev;

        prev = cur;
        if (right != nullptr) {
            inorder(right, prev, head);
        }
    }
    
public:
    Node* treeToDoublyList(Node* root) {
        if (root == nullptr) {
            return nullptr;
        }
        Node* prev = nullptr;
        Node* head = nullptr;
        inorder(root, prev, head);
        
        prev->right = head;
        head->left = prev;
        
        return head;
    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zhenchaogan.gitbook.io/leetcode-solution/leetcode-426-convert-binary-search-tree-to-sorted-doubly-linked-list.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
