Convert Binary Search Tree to Sorted Doubly Linked List - 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.

Input & Output

Example 1 — Basic BST
$ Input: root = [4,2,5,1,3]
Output: [1,2,3,4,5]
💡 Note: In-order traversal gives sorted sequence 1→2→3→4→5. Convert to circular doubly linked list where each node's left points to predecessor and right to successor.
Example 2 — Right-skewed Tree
$ Input: root = [2,1,3]
Output: [1,2,3]
💡 Note: Simple BST with three nodes. In-order gives 1→2→3, converted to circular list.
Example 3 — Single Node
$ Input: root = [1]
Output: [1]
💡 Note: Single node becomes circular list pointing to itself: left and right both point to the same node.

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

Visualization

Tap to expand
BST to Circular Doubly Linked List TransformationInput: BST42513In-Order: 1→2→3→4→512345Output: Circular Doubly Linked List12345
Understanding the Visualization
1
Input BST
Binary Search Tree with nodes [4,2,5,1,3]
2
In-Order Traversal
Visit nodes in sorted order: 1→2→3→4→5
3
Circular List
Connect as circular doubly linked list
Key Takeaway
🎯 Key Insight: In-order traversal of BST gives sorted sequence - connect nodes during traversal for optimal space
Asked in
Google 45 Facebook 38 Microsoft 32 Amazon 28
125.0K Views
Medium Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen