Last Updated: 3 Nov, 2025

Sorted Doubly Linked List to Balanced BST

Easy
Asked in company
Josh Technology Group

Problem statement

You are given a special linked list that is sorted in ascending order. Each node in this list is a doubly linked list node, containing data, a next pointer to the next node, and a prev pointer to the previous node.


Your task is to convert this sorted doubly linked list into a height-balanced Binary Search Tree (BST).


A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one. The BST property must also be maintained: for any given node, all values in its left subtree are less than the node's value, and all values in its right subtree are greater.


Your function should take the head of the doubly linked list and return the root of the newly constructed BST.


Input Format:
The first and only line of input contains the node data for the sorted doubly linked list, separated by single spaces. The input is terminated by -1.


Output Format:
Print the level-order traversal of the constructed BST. Each level of the tree should be on a new line, with node values on the same level separated by spaces. A null child should not be printed.