Medium · Trees

Convert Binary Search Tree to Sorted Doubly Linked List

Given the root of a binary search tree with distinct values, rearrange its nodes in place into a sorted circular doubly linked list (left points to the previous node, right to the next, and the largest and smallest nodes point to each other) and return the smallest node, or null for an empty tree.

Examples

Example 1

{
  "tree": [4, 2, 5, 1, 3]
}

Output: 1 ⇄ 2 ⇄ 3 ⇄ 4 ⇄ 5

Example 2

{
  "tree": [3, 1, 4, null, 2, null, 5]
}

Output: 1 ⇄ 2 ⇄ 3 ⇄ 4 ⇄ 5

Rebuild it in the studio

Open every published interview problem, solution, scene, and memory room free, with no account.

More Trees problems