Open In App

Convert given Binary Tree to Doubly Linked List in Linear time

Last Updated : 23 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given a Binary Tree (BT), the task is to convert it to a Doubly Linked List (DLL) in place. The left and right pointers in nodes will be used as previous and next pointers respectively in converted DLL. The order of nodes in DLL must be the same as the order of the given Binary Tree. The first node of Inorder traversal (leftmost node in BT) must be the head node of the DLL.

Examples:

Input:

Convert-Binary-Tree-to-Doubly-Linked-List-using-inorder-traversal-ex-1

Output:

Convert-Binary-Tree-to-Doubly-Linked-List-using-inorder-traversal-1


Explanation: The above binary tree is converted into doubly linked list where left pointer of the binary tree node act as the previous node and right pointer of the binary tree node act as the next node.


Input:

Convert-Binary-Tree-to-Doubly-Linked-List-using-inorder-traversal-ex-2

Output:

Convert-Binary-Tree-to-Doubly-Linked-List-using-inorder-traversal-2


Explanation: The above binary tree is converted into doubly linked list where left pointer of the binary tree node act as the previous node and right pointer of the binary tree node act as the next node.

Approach:

In the following implementation, we traverse the tree in inorder fashion. We add nodes at the beginning of current linked list and update head of the list using pointer to head pointer. Since we insert at the beginning, we need to process leaves in reverse order. For reverse order, we first traverse the right subtree before the left subtree. i.e. do a reverse inorder traversal. 

C++
// C++ program to convert a given Binary 
// Tree to Doubly Linked List

#include <bits/stdc++.h>
using namespace std;

class Node {
  
      public:
    int data;
    Node *left, *right;
      Node(int x) {
         data = x;
          left = right = nullptr;
    }
};

// A simple recursive function to convert a given
// Binary tree to Doubly Linked List
// root    --> Root of Binary Tree
// head --> Pointer to head node of created doubly linked list
void BToDLL(Node* root, Node*& head) {
  
    // Base cases
    if (root == nullptr)
        return;

    // Recursively convert right subtree
    BToDLL(root->right, head);

    // insert root into DLL
    root->right = head;

    // Change left pointer of previous head
    if (head != nullptr)
        head->left = root;

    // Change head of Doubly linked list
    head = root;

    // Recursively convert left subtree
    BToDLL(root->left, head);
}


Node* bToDLL(Node* root) {
      
    Node* head = nullptr;
    BToDLL(root, head);
    return head;
}

void printList(Node* head) {
  
    while (head) {
        cout<< head->data << " ";
        head = head->right;
    }
}


int main() {
  
     // Constructing below tree 
     //         5 
     //        / \ 
     //      3     6 
     //     / \     \ 
     //     1  4     8 
     //    / \      / \ 
     //    0 2      7  9
    Node* root = new Node(5);
    root->left = new Node(3);
    root->right = new Node(6);
    root->left->left = new Node(1);
    root->left->right = new Node(4);
    root->right->right = new Node(8);
    root->left->left->left = new Node(0);
    root->left->left->right = new Node(2);
    root->right->right->left = new Node(7);
    root->right->right->right = new Node(9);
  
      Node*head = bToDLL(root);
      printList(head);

    return 0;
}
Java Python C# JavaScript

Output
0 1 2 3 4 5 6 7 8 9 

Time Complexity: O(n), as the solution does a single traversal of given Binary Tree.
Auxiliary Space: O(n)

Related articles:



Next Article

Similar Reads

three90RightbarBannerImg