http://www.geeksforgeeks.org/in-place-conversion-of-sorted-dll-to-balanced-bst/
Convert a sorted doubly linked list into a BST
Tim: O(N)We construct from leaves to root. The idea is to insert nodes in BST in the same order as the appear in Doubly Linked List, so that the tree can be constructed in O(n) time complexity.
We first count the number of nodes in the given Linked List. Let the count be n. After counting nodes, we take left n/2 nodes and recursively construct the left subtree. After left subtree is constructed, we assign middle node to root and link the left subtree with root. Finally, we recursively construct the right subtree and link it with root.
While constructing the BST, we also keep moving the list head pointer to next so that we have the appropriate pointer in each recursive call.
struct
Node* sortedListToBST(
struct
Node *head)
{
/*Count the number of nodes in Linked List */
int
n = countNodes(head);
/* Construct BST */
return
sortedListToBSTRecur(&head, n);
}
/* The main function that constructs balanced BST and returns root of it.
head_ref --> Pointer to pointer to head node of Doubly linked list
n --> No. of nodes in the Doubly Linked List */
struct
Node* sortedListToBSTRecur(
struct
Node **head_ref,
int
n)
{
/* Base Case */
if
(n <= 0)
return
NULL;
/* Recursively construct the left subtree */
struct
Node *left = sortedListToBSTRecur(head_ref, n/2);
/* head_ref now refers to middle node, make middle node as root of BST*/
struct
Node *root = *head_ref;
// Set pointer to left subtree
root->prev = left;
/* Change head pointer of Linked List for parent recursive calls */
*head_ref = (*head_ref)->next;
/* Recursively construct the right subtree and link it with root
The number of nodes in right subtree is total nodes - nodes in
left subtree - 1 (for root) */
root->next = sortedListToBSTRecur(head_ref, n-n/2-1);
return
root;
}
SortedListToBST.java
private static NodeT<Integer> head;// Returns the root of the corresponding BST. The prev and next
// fields of the list nodes are used as the BST nodes left and right fields.
public static NodeT<Integer> buildBSTFromSortedDoublyLinkedList(
NodeT<Integer> L, int n) {
head = L;
return buildSortedDoublyLinkedListHelper(0, n);
}
// Builds a BST from the (s + 1)-th to the e-th node in L, and returns the
// root. Node numbering is from 1 to n.
private static NodeT<Integer> buildSortedDoublyLinkedListHelper(int s,
int e) {
if (s >= e) {
return null;
}
int m = s + ((e - s) / 2);
NodeT<Integer> left = buildSortedDoublyLinkedListHelper(s, m);
// The last function call sets L to the successor of the maximum node in
// the tree rooted at left.
NodeT<Integer> curr = new NodeT<>(head.getData());
head = head.getNext();
curr.setPrev(left);
curr.setNext(buildSortedDoublyLinkedListHelper(m + 1, e));
return curr;
}
Method 1 (Simple)
1) Get the Middle of the linked list and make it root.
2) Recursively do same for left half and right half. a) Get the middle of left half and make it left child of the root created in step 1. b) Get the middle of right half and make it right child of the root created in step 1.Read full article from In-place conversion of Sorted DLL to Balanced BST | GeeksforGeeksConvert Sorted List to Balanced Binary Search Tree (BST)
Time complexity: O(nLogn) where n is the number of nodes in Linked List.