Given a sorted doubly linked list, in-place convert it into a height-balanced binary search tree (BST). The difference between the height of the left and right subtree for every node of a height-balanced BST is never greater than 1.

The conversion should be done such that the previous child pointer of a doubly-linked list node should act as a left pointer for a binary tree node, and the next child pointer should act as the right pointer for a binary tree node. The conversion should also be done by only exchanging the pointers without allocating any memory for the BST nodes.

For example,

Height Balanced BST

Practice this problem

A simple solution would be to traverse the doubly linked list, store every node in an array, and then construct a height-balanced BST from nodes in the array. The idea is to make the middle node in the sorted array as the BST’s root node. All nodes before the middle node will go in the left subtree, and all nodes after the middle node will go in the right subtree. If we follow this recursively for the left and right subtree, we will get a height-balanced BST. Following is the pictorial representation of this approach, followed by the C++, Java, and Python implementation:

 
Convert sorted doubly linked list into a balanced BST

C++


Download  Run Code

Output:

Doubly Linked List: 8 10 12 15 18 20 25
Preorder traversal of the constructed BST: 15 10 8 12 20 18 25

Java


Download  Run Code

Output:

Doubly Linked List: 8 10 12 15 18 20 25
Preorder traversal of the constructed BST: 15 10 8 12 20 18 25

Python


Download  Run Code

Output:

Doubly Linked List: 8 10 12 15 18 20 25
Preorder traversal of the constructed BST: 15 10 8 12 20 18 25

The time complexity of the above solution is O(n), where n is the size of the BST. However, it requires O(n) extra space for storing the BST nodes. We can do the conversion in-place without any auxiliary data structure.

 
The idea is to build the BST in an inorder fashion, i.e., the same order as nodes appear in the doubly linked list. We start by counting the total number of nodes in the doubly linked list. Then recursively construct the left subtree with the first half of nodes in a doubly-linked list, assign the middle node of the doubly linked list to the BST’s root node, and set the constructed left subtree as the left child of the root node. To get the middle node in constant time, move the head pointer of the doubly linked list to the next node after setting the BST’s root node. Finally, recursively construct the right subtree with remaining nodes in the doubly linked list and set it as the root node’s right child.

The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

Doubly Linked List: 8 10 12 15 18 20 25
Preorder traversal of the constructed BST: 15 10 8 12 20 18 25

Java


Download  Run Code

Output:

Doubly Linked List: 8 10 12 15 18 20 25
Preorder traversal of the constructed BST: 15 10 8 12 20 18 25

Python


Download  Run Code

Output:

Doubly Linked List: 8 10 12 15 18 20 25
Preorder traversal of the constructed BST: 15 10 8 12 20 18 25