Open In App

Convert Binary Tree to Doubly Linked List by fixing left and right pointers

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

Given a Binary Tree, 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 (the leftmost node in Binary Tree) 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:

The idea involves converting a Binary Tree to a Doubly Linked List (DLL) in place by first fixing the left pointers during an in-order traversal to point to the previous node, then fixing the right pointers by traversing the modified tree in reverse using the left pointers to point to the next node in the DLL.

Step by step approach:

  • Perform an in-order traversal of the tree to fix the left pointers, updating each node’s left to point to the previously visited node.
  • Use the rightmost node (last node in DLL) and traverse back using left pointers to fix the right pointers for each node.
  • Ensure the leftmost node (first in DLL) is returned as the head of the DLL.
C++
// C++ program for in-place
// conversion of Binary Tree to DLL
#include <bits/stdc++.h>
using namespace std;

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

// function to fix left pointers
void fixPrevPtr(Node* root, Node*& prev) {
    if (!root) return;

    // Recur for left subtree
    fixPrevPtr(root->left, prev);

    // Update left pointer
    root->left = prev;

    // Update previous pointer
    prev = root;

    // Recur for right subtree
    fixPrevPtr(root->right, prev);
}

// function to fix right pointers and get the head
Node* fixNextPtr(Node* root) {
    
    // Find the rightmost node
    Node* prev = nullptr;
    while (root && root->right) root = root->right;

    // Traverse back using left 
    // pointers to fix right pointers
    while (root) {
        root->right = prev;
        prev = root;
        root = root->left;
    }

    // Return the head (leftmost node)
    return prev;
}

// The main function that converts 
// Binary Tree to DLL and returns head of DLL 
Node* bToDLL(Node* root) { 
    if (!root) return nullptr;

    Node* prev = nullptr;

    // Fix the left pointers
    fixPrevPtr(root, prev);

    // Fix the right pointers 
    // and return the head
    return fixNextPtr(root);
} 

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

int main() {
    
    // Create a hard coded binary tree
    //          10
    //         /  \
    //       12    15    
    //      / \    /
    //     25 30  36
    Node* root = new Node(10);
    root->left = new Node(12);
    root->right = new Node(15);
    root->left->left = new Node(25);
    root->left->right = new Node(30);
    root->right->left = new Node(36);

    Node* head = bToDLL(root);

    printList(head);

    return 0;
}
C Java Python C# JavaScript

Output
25 12 30 10 36 15 

Time Complexity: O(n), where n is the number of nodes in Binary tree.
Auxiliary Space: O(n)

Related Article:



Next Article

Similar Reads

three90RightbarBannerImg