Problem Statement
Given a sorted Doubly Linked List, convert it into a height-balanced Binary Search Tree (BST).
Constraint: You must NOT create new nodes. Reuse the existing DLL nodes — the prev pointer becomes left child and next pointer becomes right child.
Constraints
1 <= number of nodes <= 10^5- DLL is sorted in ascending order
- No new nodes allowed — rearrange pointers only
- Result must be height-balanced (height difference between subtrees ≤ 1)
Example
Input DLL: 1 ↔ 2 ↔ 3 ↔ 4 ↔ 5 ↔ 6 ↔ 7
Output BST:
4
/ \
2 6
/ \ / \
1 3 5 7
Where left = prev pointer, right = next pointer.
What the Interviewer Expects
- Find the middle node — this becomes the root (ensures balance).
- Recursively build left and right subtrees — left half of DLL becomes left subtree, right half becomes right subtree.
- Two approaches:
- Top-down: find middle each time (O(n log n))
- Bottom-up: advance a pointer as you build (O(n)) — similar to sortedListToBST on LeetCode
- Pointer reassignment —
node.prev = leftChild,node.next = rightChild. Break DLL links properly. - Height-balanced proof — always picking the middle guarantees balance.
Follow-ups
- Can you do this in O(n) time? (Bottom-up approach without finding middle repeatedly)
- What if the DLL is circular? How do you determine length/start?
- What if the linked list is singly linked? What changes?
- How would you verify the output is a valid BST AND height-balanced?