Open In App

In-place conversion of Sorted DLL to Balanced BST

Improve
Improve
Like Article
Like
Save
Share
Report

Given a Doubly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Doubly Linked List. The tree must be constructed in-place (No new node should be allocated for tree conversion) 

Examples: 

Input:  Doubly Linked List 1  2  3
Output: A Balanced BST 
     2   
   /  \  
  1    3 


Input: Doubly Linked List 1  2 3  4 5  6  7
Output: A Balanced BST
        4
      /   \
     2     6
   /  \   / \
  1   3  5   7  

Input: Doubly Linked List 1  2  3  4
Output: A Balanced BST
      3   
    /  \  
   2    4 
 / 
1

Input:  Doubly Linked List 1  2  3  4  5  6
Output: A Balanced BST
      4   
    /   \  
   2     6 
 /  \   / 
1   3  5   

The Doubly Linked List conversion is very much similar to this Singly Linked List problem and the method 1 is exactly same as the method 1 of previous post. Method 2 is also almost same. The only difference in method 2 is, instead of allocating new nodes for BST, we reuse same DLL nodes. We use prev pointer as left and next pointer as right.

Method 1 (Simple) 
Following is a simple algorithm where we first find the middle node of list and make it root of the tree to be constructed.  

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.

Time complexity: O(nLogn) where n is the number of nodes in Linked List.

Method 2 (Tricky) 
The 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 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. 

Following is the implementation of method 2. The main code which creates Balanced BST is highlighted.  

C++




#include <bits/stdc++.h>
using namespace std;
 
/* A Doubly Linked List node that
will also be used as a tree node */
class Node
{
    public:
    int data;
 
    // For tree, next pointer can be
    // used as right subtree pointer
    Node* next;
 
    // For tree, prev pointer can be
    // used as left subtree pointer
    Node* prev;
};
 
// A utility function to count nodes in a Linked List
int countNodes(Node *head);
 
Node* sortedListToBSTRecur(Node **head_ref, int n);
 
/* This function counts the number of
nodes in Linked List and then calls
sortedListToBSTRecur() to construct BST */
Node* sortedListToBST(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 */
Node* sortedListToBSTRecur(Node **head_ref, int n)
{
    /* Base Case */
    if (n <= 0)
        return NULL;
 
    /* Recursively construct the left subtree */
    Node *left = sortedListToBSTRecur(head_ref, n/2);
 
    /* head_ref now refers to middle node,
    make middle node as root of BST*/
    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;
}
 
/* UTILITY FUNCTIONS */
/* A utility function that returns
count of nodes in a given Linked List */
int countNodes(Node *head)
{
    int count = 0;
    Node *temp = head;
    while(temp)
    {
        temp = temp->next;
        count++;
    }
    return count;
}
 
/* Function to insert a node at
the beginning of the Doubly Linked List */
void push(Node** head_ref, int new_data)
{
    /* allocate node */
    Node* new_node = new Node();
 
    /* put in the data */
    new_node->data = new_data;
 
    /* since we are adding at the beginning,
    prev is always NULL */
    new_node->prev = NULL;
 
    /* link the old list of the new node */
    new_node->next = (*head_ref);
 
    /* change prev of head node to new node */
    if((*head_ref) != NULL)
    (*head_ref)->prev = new_node ;
 
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
 
/* Function to print nodes in a given linked list */
void printList(Node *node)
{
    while (node!=NULL)
    {
        cout<<node->data<<" ";
        node = node->next;
    }
}
 
/* A utility function to print
preorder traversal of BST */
void preOrder(Node* node)
{
    if (node == NULL)
        return;
    cout<<node->data<<" ";
    preOrder(node->prev);
    preOrder(node->next);
}
 
/* Driver code*/
int main()
{
    /* Start with the empty list */
    Node* head = NULL;
 
    /* Let us create a sorted linked list to test the functions
    Created linked list will be 7->6->5->4->3->2->1 */
    push(&head, 7);
    push(&head, 6);
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);
 
    cout<<"Given Linked List\n";
    printList(head);
 
    /* Convert List to BST */
    Node *root = sortedListToBST(head);
    cout<<"\nPreOrder Traversal of constructed BST \n ";
    preOrder(root);
 
    return 0;
}
 
// This code is contributed by rathbhupendra


C




#include<stdio.h>
#include<stdlib.h>
 
/* A Doubly Linked List node that will also be used as a tree node */
struct Node
{
    int data;
 
    // For tree, next pointer can be used as right subtree pointer
    struct Node* next;
 
    // For tree, prev pointer can be used as left subtree pointer
    struct Node* prev;
};
 
// A utility function to count nodes in a Linked List
int countNodes(struct Node *head);
 
struct Node* sortedListToBSTRecur(struct Node **head_ref, int n);
 
/* This function counts the number of nodes in Linked List and then calls
   sortedListToBSTRecur() to construct BST */
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;
}
 
/* UTILITY FUNCTIONS */
/* A utility function that returns count of nodes in a given Linked List */
int countNodes(struct Node *head)
{
    int count = 0;
    struct Node *temp = head;
    while(temp)
    {
        temp = temp->next;
        count++;
    }
    return count;
}
 
/* Function to insert a node at the beginning of the Doubly Linked List */
void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct Node* new_node =
            (struct Node*) malloc(sizeof(struct Node));
 
    /* put in the data  */
    new_node->data  = new_data;
 
    /* since we are adding at the beginning,
      prev is always NULL */
    new_node->prev = NULL;
 
    /* link the old list of the new node */
    new_node->next = (*head_ref);
 
    /* change prev of head node to new node */
    if((*head_ref) !=  NULL)
      (*head_ref)->prev = new_node ;
 
    /* move the head to point to the new node */
    (*head_ref)    = new_node;
}
 
/* Function to print nodes in a given linked list */
void printList(struct Node *node)
{
    while (node!=NULL)
    {
        printf("%d ", node->data);
        node = node->next;
    }
}
 
/* A utility function to print preorder traversal of BST */
void preOrder(struct Node* node)
{
    if (node == NULL)
        return;
    printf("%d ", node->data);
    preOrder(node->prev);
    preOrder(node->next);
}
 
/* Driver program to test above functions*/
int main()
{
    /* Start with the empty list */
    struct Node* head = NULL;
 
    /* Let us create a sorted linked list to test the functions
     Created linked list will be 7->6->5->4->3->2->1 */
    push(&head, 7);
    push(&head, 6);
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);
 
    printf("Given Linked List\n");
    printList(head);
 
    /* Convert List to BST */
    struct Node *root = sortedListToBST(head);
    printf("\n PreOrder Traversal of constructed BST \n ");
    preOrder(root);
 
    return 0;
}


Java




class Node
{
    int data;
    Node next, prev;
 
    Node(int d)
    {
        data = d;
        next = prev = null;
    }
}
 
class LinkedList
{
    Node head;
 
    /* This function counts the number of nodes in Linked List
       and then calls sortedListToBSTRecur() to construct BST */
    Node sortedListToBST()
    {
        /*Count the number of nodes in Linked List */
        int n = countNodes(head);
 
        /* Construct BST */
        return sortedListToBSTRecur(n);
    }
 
    /* The main function that constructs balanced BST and
       returns root of it.
       n  --> No. of nodes in the Doubly Linked List */
    Node sortedListToBSTRecur(int n)
    {
        /* Base Case */
        if (n <= 0)
            return null;
 
        /* Recursively construct the left subtree */
        Node left = sortedListToBSTRecur(n / 2);
 
        /* head_ref now refers to middle node,
           make middle node as root of BST*/
        Node root = head;
 
        // Set pointer to left subtree
        root.prev = left;
 
        /* Change head pointer of Linked List for parent
           recursive calls */
        head = head.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(n - n / 2 - 1);
 
        return root;
    }
 
    /* UTILITY FUNCTIONS */
    /* A utility function that returns count of nodes in a
       given Linked List */
    int countNodes(Node head)
    {
        int count = 0;
        Node temp = head;
        while (temp != null)
        {
            temp = temp.next;
            count++;
        }
        return count;
    }
 
    /* Function to insert a node at the beginning of
       the Doubly Linked List */
    void push(int new_data)
    {
        /* allocate node */
        Node new_node = new Node(new_data);
 
        /* since we are adding at the beginning,
           prev is always NULL */
        new_node.prev = null;
 
        /* link the old list of the new node */
        new_node.next = head;
 
        /* change prev of head node to new node */
        if (head != null)
            head.prev = new_node;
 
        /* move the head to point to the new node */
        head = new_node;
    }
 
    /* Function to print nodes in a given linked list */
    void printList()
    {
        Node node = head;
        while (node != null)
        {
            System.out.print(node.data + " ");
            node = node.next;
        }
    }
 
    /* A utility function to print preorder traversal of BST */
    void preOrder(Node node)
    {
        if (node == null)
            return;
        System.out.print(node.data + " ");
        preOrder(node.prev);
        preOrder(node.next);
    }
 
    /* Driver program to test above functions */
    public static void main(String[] args)
    {
        LinkedList llist = new LinkedList();
 
        /* Let us create a sorted linked list to test the functions
           Created linked list will be 7->6->5->4->3->2->1 */
        llist.push(7);
        llist.push(6);
        llist.push(5);
        llist.push(4);
        llist.push(3);
        llist.push(2);
        llist.push(1);
 
        System.out.println("Given Linked List ");
        llist.printList();
 
        /* Convert List to BST */
        Node root = llist.sortedListToBST();
        System.out.println("");
        System.out.println("Pre-Order Traversal of constructed BST ");
        llist.preOrder(root);
    }
}
// This code has been contributed by Mayank Jaiswal(mayank_24)


Python3




# Python program for In-place conversion of Sorted DLL to Balanced BST
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
        self.prev = None
 
 
class LinkedList:
    def __init__(self):
        self.head = None
 
    # This function counts the number of nodes in Linked List
    # and then calls sortedListToBSTRecur() to construct BST
    def sortedListToBST(self):
        # Count the number of nodes in Linked List
        n = self.countNodes(self.head)
         
        # Construct BST
        return self.sortedListToBSTRecur(n)
 
    # The main function that constructs balanced BST and returns
    # root of it. n  --> No. of nodes in the Doubly Linked List
    def sortedListToBSTRecur(self, n):
        # base case
        if n <= 0
            return None
 
        # Recursively construct the left subtree
        left = self.sortedListToBSTRecur(n // 2)
 
        # head_ref now refers to middle node, make middle node as root of BST
        root = self.head
 
        # Set pointer to left subtree
        root.prev = left
 
        # Change head pointer of Linked List for parent recursive calls
        self.head = self.head.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 = self.sortedListToBSTRecur(n - n // 2 - 1)
 
        return root 
 
    # UTILITY FUNCTIONS
    # A utility function that returns count of nodes in a
    #   given Linked List
    def countNodes(self, head):
        count = 0
        temp = head
 
        while temp != None:
            temp = temp.next
            count += 1
        return count
 
    # Function to insert a node at the beginning of
    #   the Doubly Linked List
    def push(self, new_data):
        # allocate node
        new_node = Node(new_data)
         
        # since we are adding at the beginning,
        #   prev is always NULL
        new_node.prev = None
         
        # link the old list of the new node
        new_node.next = self.head
 
        # change prev of head node to new node
        if self.head != None:
            self.head.prev = new_node;  
 
        # move the head to point to the new node
        self.head = new_node;    
         
    # Function to print nodes in a given linked list
    def printList(self):    
        node = self.head;    
        while node != None:        
            print(node.data, end=" ")        
            node = node.next
 
    # A utility function to print preorder traversal of BST
    def preOrder(self, node):
        if node == None:
            return
         
        print(node.data, end=" ")
        self.preOrder(node.prev)
        self.preOrder(node.next)
 
# Driver program to test above functions
if __name__ == '__main__':
    llist = LinkedList()
     
    # Let us create a sorted linked list to test the functions
    # Created linked list will be 7->6->5->4->3->2->1
    llist.push(7);
    llist.push(6)
    llist.push(5)
    llist.push(4)
    llist.push(3)
    llist.push(2)
    llist.push(1)
     
    print("Given Linked List ")
    llist.printList()
     
    # Convert List to BST
    root = llist.sortedListToBST()
    print("")
    print("Pre-Order Traversal of constructed BST ")
    llist.preOrder(root);
     
# This code is contributed by Tapesh(tapeshdua420)


C#




using System;
public class Node {
  public int data;
  public Node next, prev;
 
  public Node(int d) {
    data = d;
    next = prev = null;
  }
}
 
public class List {
  Node head;
 
  /*
     * This function counts the number of nodes in Linked List and then calls
     * sortedListToBSTRecur() to construct BST
     */
  Node sortedListToBST() {
    /* Count the number of nodes in Linked List */
    int n = countNodes(head);
 
    /* Construct BST */
    return sortedListToBSTRecur(n);
  }
 
  /*
     * The main function that constructs balanced BST and returns root of it. n -->
     * No. of nodes in the Doubly Linked List
     */
  Node sortedListToBSTRecur(int n) {
    /* Base Case */
    if (n <= 0)
      return null;
 
    /* Recursively construct the left subtree */
    Node left = sortedListToBSTRecur(n / 2);
 
    /*
         * head_ref now refers to middle node, make middle node as root of BST
         */
    Node root = head;
 
    // Set pointer to left subtree
    root.prev = left;
 
    /*
         * Change head pointer of Linked List for parent recursive calls
         */
    head = head.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(n - n / 2 - 1);
 
    return root;
  }
 
  /* UTILITY FUNCTIONS */
  /*
     * A utility function that returns count of nodes in a given Linked List
     */
  int countNodes(Node head) {
    int count = 0;
    Node temp = head;
    while (temp != null) {
      temp = temp.next;
      count++;
    }
    return count;
  }
 
  /*
     * Function to insert a node at the beginning of the Doubly Linked List
     */
  void Push(int new_data) {
    /* allocate node */
    Node new_node = new Node(new_data);
 
    /*
         * since we are adding at the beginning, prev is always NULL
         */
    new_node.prev = null;
 
    /* link the old list of the new node */
    new_node.next = head;
 
    /* change prev of head node to new node */
    if (head != null)
      head.prev = new_node;
 
    /* move the head to point to the new node */
    head = new_node;
  }
 
  /* Function to print nodes in a given linked list */
  void printList() {
    Node node = head;
    while (node != null) {
      Console.Write(node.data + " ");
      node = node.next;
    }
  }
 
  /* A utility function to print preorder traversal of BST */
  void preOrder(Node node) {
    if (node == null)
      return;
    Console.Write(node.data + " ");
    preOrder(node.prev);
    preOrder(node.next);
  }
 
  /* Driver program to test above functions */
  public static void Main(String[] args) {
    List llist = new List();
 
    /*
         * Let us create a sorted linked list to test the functions Created linked list
         * will be 7->6->5->4->3->2->1
         */
    llist.Push(7);
    llist.Push(6);
    llist.Push(5);
    llist.Push(4);
    llist.Push(3);
    llist.Push(2);
    llist.Push(1);
 
    Console.WriteLine("Given Linked List ");
    llist.printList();
 
    /* Convert List to BST */
    Node root = llist.sortedListToBST();
    Console.WriteLine("");
    Console.WriteLine("Pre-Order Traversal of constructed BST ");
    llist.preOrder(root);
  }
}
 
// This code is contributed by Rajput-Ji


Javascript




<script>
 
class Node
{
    constructor(d)
    {
        this.data=d;
        this.next=this.prev=null;
    }
}
 
let head;
 
/* This function counts the number of nodes in Linked List
       and then calls sortedListToBSTRecur() to construct BST */
function sortedListToBST()
{
    /*Count the number of nodes in Linked List */
        let n = countNodes(head);
  
        /* Construct BST */
        return sortedListToBSTRecur(n);
}
 
/* The main function that constructs balanced BST and
       returns root of it.
       n  --> No. of nodes in the Doubly Linked List */
function sortedListToBSTRecur(n)
{
    /* Base Case */
        if (n <= 0)
            return null;
  
        /* Recursively construct the left subtree */
        let left = sortedListToBSTRecur(Math.floor(n / 2));
  
        /* head_ref now refers to middle node,
           make middle node as root of BST*/
        let root = head;
  
        // Set pointer to left subtree
        root.prev = left;
  
        /* Change head pointer of Linked List for parent
           recursive calls */
        head = head.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(n - Math.floor(n / 2) - 1);
  
        return root;
}
 
/* UTILITY FUNCTIONS */
    /* A utility function that returns count of nodes in a
       given Linked List */
function countNodes(head)
{
    let count = 0;
        let temp = head;
        while (temp != null)
        {
            temp = temp.next;
            count++;
        }
        return count;
}
 
/* Function to insert a node at the beginning of
       the Doubly Linked List */
function push(new_data)
{
    /* allocate node */
        let new_node = new Node(new_data);
  
        /* since we are adding at the beginning,
           prev is always NULL */
        new_node.prev = null;
  
        /* link the old list of the new node */
        new_node.next = head;
  
        /* change prev of head node to new node */
        if (head != null)
            head.prev = new_node;
  
        /* move the head to point to the new node */
        head = new_node;
}
 
/* Function to print nodes in a given linked list */
function printList()
{
    let node = head;
        while (node != null)
        {
            document.write(node.data + " ");
            node = node.next;
        }
}
 
/* A utility function to print preorder traversal of BST */
function preOrder(node)
{
    if (node == null)
            return;
        document.write(node.data + " ");
        preOrder(node.prev);
        preOrder(node.next);
}
 
/* Driver program to test above functions */
/* Let us create a sorted linked list to test the functions
           Created linked list will be 7->6->5->4->3->2->1 */
push(7);
push(6);
push(5);
push(4);
push(3);
push(2);
push(1);
 
document.write("Given Linked List <br>");
printList();
 
/* Convert List to BST */
let root = sortedListToBST();
document.write("<br>");
document.write("Pre-Order Traversal of constructed BST <br>");
preOrder(root);
     
// This code is contributed by avanitrachhadiya2155
</script>


Output

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)
Auxiliary Space: O(1)

Another Approach(using extra space):
Follow the below steps to solve this problem:
1) Create a array and store all the elements of linked list.
2) Now find the middle element of the linked list and create it root of the tree and call for left array and right array for left and right child.
3) Now recursively repeat above approach until the start becomes greater than end.
4) Now print the preorder traversal of created tree.

Below is the implementation of above approach:

C++




// C++ implementation of above approach
#include<bits/stdc++.h>
using namespace std;
 
// link list node
struct LNode{
    int data;
    LNode* next;
    LNode* prev;
};
   
// binary tree node
struct TNode{
    int data;
    TNode* left;
    TNode* right;
    TNode(int data){
        this->data = data;
        this->left = NULL;
        this->right = NULL;
    }
};
 
// function to insert a node at
// the beginning of the doubly linked list
void push(LNode** head_ref, int new_data){
    // allocated node
    LNode* new_node = new LNode();
     
    // put in the data
    new_node->data = new_data;
     
    // since we are adding at the beginning
    // prev is always null
    new_node->prev = NULL;
     
    // link the old list of the new node
    new_node->next = (*head_ref);
     
    // changes prev of head node to new node
    if(*head_ref != NULL)
        (*head_ref)->prev = new_node;
     
    // move the head to point to the new node
    *head_ref = new_node;
}
 
// function to print nodes in a given linked list
void printList(LNode* node){
    while(node != NULL){
        cout<<node->data<<" ";
        node = node->next;
    }
}
 
void preOrder(TNode* root){
    if(root == NULL) return;
    cout<<root->data<<" ";
    preOrder(root->left);
    preOrder(root->right);
}
TNode* sortedListToBSTRecur(vector<int>& vec, int start, int end){
    if(start > end) return NULL;
    int mid = start + (end-start)/2;
    if((end - start)%2 != 0) mid = mid+1;
    TNode* root = new TNode(vec[mid]);
    root->left = sortedListToBSTRecur(vec, start, mid-1);
    root->right = sortedListToBSTRecur(vec, mid+1, end);
    return root;
}
 
TNode* sortedListToBST(LNode* head){
    vector<int> vec;
    LNode* temp = head;
    while(temp != NULL){
        vec.push_back(temp->data);
        temp = temp->next;
    }
    return sortedListToBSTRecur(vec, 0, vec.size()-1);
}
 
int main(){
    // Let us create a sorted linked list to test the functions
    // Created linked list will be 1->2->3->4->5->6->7
    LNode* head = NULL;
    push(&head, 7);
    push(&head, 6);
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);
     
    cout<<"Given Linked List: "<<endl;
    printList(head);
    cout<<endl;
    // convert list to bst
    TNode* root = sortedListToBST(head);
    cout<<"Peorder Traversal of constructed BST: "<<endl;
    preOrder(root);
    return 0;
}
// THIS CODE IS CONTRIBUTED BY YASH AGARWAL(YASHAGARWAL2852002)


Java




import java.util.ArrayList;
import java.util.List;
 
// Linked list node
class LNode {
    int data;
    LNode next;
    LNode prev;
}
 
// Binary tree node
class TNode {
    int data;
    TNode left;
    TNode right;
 
    TNode(int item) {
        data = item;
        left = null;
        right = null;
    }
}
 
class Main {
    static LNode push(LNode head_ref, int new_data) {
        LNode new_node = new LNode();
        new_node.data = new_data;
        new_node.prev = null;
        new_node.next = head_ref;
 
        if (head_ref != null)
            head_ref.prev = new_node;
 
        head_ref = new_node;
        return head_ref;
    }
 
    static void printList(LNode node) {
        while (node != null) {
            System.out.print(node.data + " ");
            node = node.next;
        }
    }
 
    static void preOrder(TNode root) {
        if (root == null)
            return;
        System.out.print(root.data + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
 
    static TNode sortedListToBSTRecur(List<Integer> vec, int start, int end) {
        if (start > end)
            return null;
 
        int mid = (start + end) / 2;
        if ((end - start) % 2 != 0)
            mid = mid + 1;
 
        TNode root = new TNode(vec.get(mid));
        root.left = sortedListToBSTRecur(vec, start, mid - 1);
        root.right = sortedListToBSTRecur(vec, mid + 1, end);
 
        return root;
    }
 
    static TNode sortedListToBST(LNode head) {
        List<Integer> vec = new ArrayList<Integer>();
        LNode temp = head;
 
        while (temp != null) {
            vec.add(temp.data);
            temp = temp.next;
        }
 
        return sortedListToBSTRecur(vec, 0, vec.size() - 1);
    }
 
    public static void main(String[] args) {
        LNode head = null;
        head = push(head, 7);
        head = push(head, 6);
        head = push(head, 5);
        head = push(head, 4);
        head = push(head, 3);
        head = push(head, 2);
        head = push(head, 1);
 
        System.out.print("Given Linked List: ");
        printList(head);
        System.out.println();
 
        TNode root = sortedListToBST(head);
        System.out.print("Preorder traversal of constructed BST: ");
        preOrder(root);
    }
}


Python




# Python implementation of above approach
# link list node
class LNode:
    def __init__(self, data):
        self.data = data
        self.next = None
        self.prev = None
     
 
class TNode:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
         
     
# function to insert a node at
# the beginning of the doubly linked list
def push(head_ref, new_data):
    # allocated node and put data
    new_node = LNode(new_data)
     
    # link the old list of the new node
    new_node.next = head_ref
     
    # changes prev of head node to new node
    if(head_ref is not None):
        head_ref.prev = new_node
     
    # move the head to point to the new node
    head_ref = new_node
    return head_ref
 
 
# function to print nodes in a given linked list
def printList(node):
    while(node is not None):
        print(node.data)
        node = node.next
     
 
def preOrder(root):
    if(root is None):
        return
    print(root.data)
    preOrder(root.left)
    preOrder(root.right)
 
 
def sortedListToBSTRecur(vec, start, end):
    if(start > end):
        return None
    mid = start + (int)((end-start)/2)
    if((end-start)%2 != 0):
        mid = mid+1
    root = TNode(vec[mid])
    root.left = sortedListToBSTRecur(vec, start, mid-1)
    root.right = sortedListToBSTRecur(vec, mid+1, end)
    return root
 
 
def sortedListToBST(head):
    vec = []
    temp = head
    while(temp is not None):
        vec.append(temp.data)
        temp = temp.next
    return sortedListToBSTRecur(vec, 0, len(vec)-1)
 
 
head = None
head = push(head, 7)
head = push(head, 6)
head = push(head, 5)
head = push(head, 4)
head = push(head, 3)
head = push(head, 2)
head = push(head, 1)
 
print("Given Linked List : ")
printList(head)
 
# convert list to bst
root = sortedListToBST(head)
print("PreOrder traversal of constructed BST : ")
preOrder(root)


C#




// C# Program to find sum of all right leaves
 
using System;
using System.Collections.Generic;
 
// link list node
public class LNode{
    public int data;
    public LNode next;
    public LNode prev;
}
 
// binary tree node
public class TNode{
    public int data;
    public TNode left;
    public TNode right;
    public TNode(int item){
        data = item;
        left = null;
        right = null;
    }
}
 
class GFG{
    static LNode push(LNode head_ref, int new_data){
        // allcated node
        LNode new_node = new LNode();
         
        // put in the data
        new_node.data = new_data;
         
        // since we are adding at the beginning
        // prev is always null
        new_node.prev = null;
         
        // link the old list of the new node
        new_node.next = head_ref;
         
        // changes prev of head nodeto new node
        if(head_ref != null)
            head_ref.prev = new_node;
         
        // move the head to point to the new node
        head_ref = new_node;
        return head_ref;
    }
     
    // function to print nodes in given linked list
    static void printList(LNode node){
        while(node != null){
            Console.Write(node.data + " ");
            node = node.next;
        }
    }
     
    static void preOrder(TNode root){
        if(root == null) return;
        Console.Write(root.data + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
     
    static TNode sortedListToBSTRecur(List<int> vec, int start, int end){
        if(start > end) return null;
        int mid = start + (end-start)/2;
        if((end-start)%2 != 0) mid = mid+1;
        TNode root = new TNode(vec[mid]);
        root.left = sortedListToBSTRecur(vec, start, mid-1);
        root.right = sortedListToBSTRecur(vec, mid+1, end);
        return root;
    }
     
    static TNode sortedListToBST(LNode head){
        List<int> vec = new List<int>();
        LNode temp = head;
        while(temp != null){
            vec.Add(temp.data);
            temp = temp.next;
        }
        return sortedListToBSTRecur(vec, 0, vec.Count-1);
    }
     
    public static void Main(String[] args){
        // Let us create a sorted linked list to test the functions
        // Created linked list will be 1->2->3->4->5->6->7
        LNode head = null;
        head = push(head, 7);
        head = push(head, 6);
        head = push(head, 5);
        head = push(head, 4);
        head = push(head, 3);
        head = push(head, 2);
        head = push(head, 1);
         
        Console.WriteLine("Given Linked List : ");
        printList(head);
         
        Console.WriteLine("");
         
        TNode root = sortedListToBST(head);
        Console.WriteLine("Preorder traversal of constructed BST : ");
        preOrder(root);
    }
}
 
// THIS CODE IS CONTRIBUTED BY YASH AGARWAL


Javascript




// JavaScript implementation of above approach
// link list node
class LNode{
    constructor(data){
        this.data = data;
        this.next = null;
        this.prev = null;
    }
}
 
// binary tree node
class TNode{
    constructor(data){
        this.data =data;
        this.left = null;
        this.right = null;
    }
}
 
// function to insert a node at
// the beginning of the doubly linked list
function push(head_ref, new_data){
    let new_node = new LNode(new_data);
    // link the old list of the new node
    new_node.next = head_ref;
    // changes prev of head node to new node
    if(head_ref != null){
        head_ref.prev = new_node;
    }
    head_ref = new_node;
    // move the head to point to the new node
    return head_ref;
}
 
// function to print nodes in a given linked list
function printList(node){
    while(node != null){
        console.log(node.data + " ");
        node = node.next;
    }
}
 
function preOrder(root){
    if(root == null) return;
    console.log(root.data + " ");
    preOrder(root.left);
    preOrder(root.right);
}
 
function sortedListToBSTRecur(vec, start, end){
    if(start > end) return null;
    let mid = start + (end-start)/2;
    if((end - start)%2 != 0) mid = mid+1;
    let root = new TNode(vec[mid]);
    root.left = sortedListToBSTRecur(vec, start, mid-1);
    root.right = sortedListToBSTRecur(vec, mid+1, end);
    return root;
}
 
function sortedListToBST(head){
    let vec = [];
    let temp = head;
    while(temp != null){
        vec.push(temp.data);
        temp = temp.next;
    }
    return sortedListToBSTRecur(vec, 0, vec.length - 1);
}
 
// Let us create a sorted linked list to test the functions
// Created linked list will be 1->2->3->4->5->6->7
let head = null;
head = push(head, 7);
head = push(head, 6);
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
 
console.log("Given linked list : ");
printList(head);
 
// convert list to bst
let root = sortedListToBST(head);
console.log("Preorder Traversal of constructed BST: ");
preOrder(root);


Output

Given Linked List: 
1 2 3 4 5 6 7 
Peorder Traversal of constructed BST: 
4 2 1 3 6 5 7 

Time Complexity: O(N) where N is the number of nodes in given doubly linked list.
Auxiliary Space: O(N) due the extra space.



Last Updated : 06 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads