|
| 1 | +<p>Convert a <strong>Binary Search Tree</strong> to a sorted <strong>Circular Doubly-Linked List</strong> in place.</p> |
| 2 | + |
| 3 | +<p>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.</p> |
| 4 | + |
| 5 | +<p>We want to do the transformation <strong>in place</strong>. 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.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<p><img src="https://assets.leetcode.com/uploads/2018/10/12/bstdlloriginalbst.png" style="width: 100%; max-width: 300px;" /></p> |
| 11 | + |
| 12 | +<pre> |
| 13 | +<strong>Input:</strong> root = [4,2,5,1,3] |
| 14 | + |
| 15 | +<img src="https://assets.leetcode.com/uploads/2018/10/12/bstdllreturndll.png" style="width: 100%; max-width: 450px;" /> |
| 16 | +<strong>Output:</strong> [1,2,3,4,5] |
| 17 | + |
| 18 | +<strong>Explanation:</strong> The figure below shows the transformed BST. The solid line indicates the successor relationship, while the dashed line means the predecessor relationship. |
| 19 | +<img src="https://assets.leetcode.com/uploads/2018/10/12/bstdllreturnbst.png" style="width: 100%; max-width: 450px;" /> |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p><strong class="example">Example 2:</strong></p> |
| 23 | + |
| 24 | +<pre> |
| 25 | +<strong>Input:</strong> root = [2,1,3] |
| 26 | +<strong>Output:</strong> [1,2,3] |
| 27 | +</pre> |
| 28 | + |
| 29 | +<p> </p> |
| 30 | +<p><strong>Constraints:</strong></p> |
| 31 | + |
| 32 | +<ul> |
| 33 | + <li>The number of nodes in the tree is in the range <code>[0, 2000]</code>.</li> |
| 34 | + <li><code>-1000 <= Node.val <= 1000</code></li> |
| 35 | + <li>All the values of the tree are <strong>unique</strong>.</li> |
| 36 | +</ul> |
0 commit comments