Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List.
Input: Linked List 1->2->3 Output: A Balanced BST 2 / \ 1 3 Input: Linked List 1->2->3->4->5->6->7 Output: A Balanced BST 4 / \ 2 6 / \ / \ 1 3 5 7 Input: Linked List 1->2->3->4 Output: A Balanced BST 3 / \ 2 4 / 1 Input: Linked List 1->2->3->4->5->6 Output: A Balanced BST 4 / \ 2 6 / \ / 1 3 5
Method 1 (Simple)
Following is a simple algorithm where we first find the middle node of the list and make it the root of the tree to be constructed.
1) Get the Middle of the linked list and make it root. 2) Recursively do same for the left half and right half. a) Get the middle of the left half and make it left child of the root created in step 1. b) Get the middle of right half and make it the right child of the root created in step 1.
Time complexity: O(nLogn) where n is the number of nodes in Linked List.
Method 2 (Tricky)
Method 1 constructs the tree from root to leaves. In this method, we construct from leaves to root. The idea is to insert nodes in BST in the same order as they appear in 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 allocate memory for 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.
Following is implementation of method 2. The main code which creates Balanced BST is highlighted.
Given Linked List 1 2 3 4 5 6 7 PreOrder Traversal of constructed BST 4 2 1 3 6 5 7
Time Complexity: O(n)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Convert a normal BST to Balanced BST
- Sorted Array to Balanced BST
- In-place conversion of Sorted DLL to Balanced BST
- Given a linked list which is sorted, how will you insert in sorted way
- Insert value in sorted way in a sorted doubly linked list
- Merge K sorted Doubly Linked List in Sorted Order
- Find if there is a triplet in a Balanced BST that adds to zero
- Find a pair with given sum in a Balanced BST
- Split a BST into two balanced BSTs based on a value K
- Check if the Binary Tree contains a balanced BST of size K
- Flatten BST to sorted list | Decreasing order
- Flatten BST to sorted list | Increasing order
- K'th Largest Element in BST when modification to BST is not allowed
- Two nodes of a BST are swapped, correct the BST | Set-2
- Two nodes of a BST are swapped, correct the BST
- Find k-th smallest element in BST (Order Statistics in BST)
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Find the balanced node in a Linked List
- Sorted merge of two sorted doubly circular linked lists
- Sorted order printing of a given array that represents a BST