706336. Sorted Linked List to BST
Sorted Linked List to BST
Summary
Given a sorted linked list, convert it into a binary search tree (BST). The problem requires you to create a balanced BST from a given sorted linked list. This involves creating nodes for each element in the linked list and then inserting them into the BST.
Detailed Explanation
To solve this problem, we will first create a new node for each element in the linked list. Then, we will insert these nodes into the BST while maintaining its balance property. We will use an AVL tree to ensure that the resulting BST is balanced.
Here's a step-by-step breakdown of the solution:
-
Create a new node for each element in the linked list.
-
Create a function
insertNodethat takes a node and the root of the BST as arguments. This function will be used to insert nodes into the BST while maintaining its balance property.// Function to insert a node into the AVL tree Node* insertNode(Node* root, int key) { // If the tree is empty, create a new node and return it if (root == nullptr) { return (new Node(key)); } // Else, recur down the tree if (key < root->data) root->left = insertNode(root->left, key); else if (key > root->data) root->right = insertNode(root->right, key); // This node will be the leaf node and so no need to balance return root; } -
Create a function
convertListToBSTthat takes the head of the sorted linked list as an argument. This function will convert the linked list into a BST.Node* convertListToBST(Node* head) { // Base case: An empty tree is always balanced if (head == nullptr) return nullptr; // Find the middle element in the sorted linked list int mid = findMiddle(head); // Create a new node with the middle element as its data Node* root = new Node(mid); // Recursively convert the left half of the linked list into a BST and make it the left child of the current node root->left = convertListToBST(getNode(head, mid - 1)); // Recursively convert the right half of the linked list into a BST and make it the right child of the current node root->right = convertListToBST(getNode(head, mid + 1)); return root; } -
Finally, call
convertListToBSTwith the head of the sorted linked list to convert the linked list into a BST.
Time complexity: O(n log n) where n is the number of elements in the linked list. Space complexity: O(n) where n is the number of elements in the linked list.
Optimized Solutions
Java
import java.util.*;
class Node {
int data;
Node left, right;
public Node(int item) {
data = item;
left = right = null;
}
}
class AVLTree {
Node root;
// Function to insert a node into the AVL tree
Node insertNode(Node root, int key) {
if (root == null) return (new Node(key));
if (key < root.data)
root.left = insertNode(root.left, key);
else if (key > root.data)
root.right = insertNode(root.right, key);
// This node will be the leaf node and so no need to balance
return root;
}
// Function to convert a sorted linked list into a BST
Node convertListToBST(Node head) {
if (head == null)
return null;
int mid = findMiddle(head);
Node root = new Node(mid);
root.left = convertListToBST(getNode(head, mid - 1));
root.right = convertListToBST(getNode(head, mid + 1));
return root;
}
// Function to find the middle element in a sorted linked list
int findMiddle(Node head) {
Node slow = head, fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow.data;
}
// Function to get the node with a given key in a sorted linked list
Node getNode(Node head, int key) {
while (head != null && head.data != key)
head = (key < head.data) ? head.left : head.right;
return head;
}
}
public class Main {
public static void main(String[] args) {
AVLTree tree = new AVLTree();
Node head = new Node(2);
head.next = new Node(3);
head.next.next = new Node(4);
head.next.next.next = new Node(5);
// Convert the sorted linked list into a BST
Node root = tree.convertListToBST(head);
// Print the in-order traversal of the resulting BST
printInOrder(root);
}
public static void printInOrder(Node node) {
if (node == null)
return;
printInOrder(node.left);
System.out.print(node.data + " ");
printInOrder(node.right);
}
}