Convert Binary Tree to Doubly Linked List by fixing left and right pointers
Last Updated :
29 Nov, 2024
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:

Output:

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:

Output:

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
// C program for in-place
// conversion of Binary Tree to DLL
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// function to fix left pointers
void fixPrevPtr(struct Node* root, struct 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
struct Node* fixNextPtr(struct Node* root) {
// Find the rightmost node
struct Node* prev = NULL;
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
struct Node* bToDLL(struct Node* root) {
if (!root) return NULL;
struct Node* prev = NULL;
// Fix the left pointers
fixPrevPtr(root, &prev);
// Fix the right pointers
// and return the head
return fixNextPtr(root);
}
void printList(struct Node* head) {
struct Node* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->right;
}
printf("\n");
}
struct Node* createNode(int x) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = x;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
int main() {
// Create a hard coded binary tree
// 10
// / \
// 12 15
// / \ /
// 25 30 36
struct Node* root = createNode(10);
root->left = createNode(12);
root->right = createNode(15);
root->left->left = createNode(25);
root->left->right = createNode(30);
root->right->left = createNode(36);
struct Node* head = bToDLL(root);
printList(head);
return 0;
}
Java
// Java program for in-place
// conversion of Binary Tree to DLL
import java.util.ArrayList;
class Node {
int data;
Node left;
Node right;
Node(int x) {
data = x;
left = null;
right = null;
}
}
class GfG {
// function to fix left pointers
static void fixPrevPtr(Node root, Node[] prev) {
if (root == null) return;
// Recur for left subtree
fixPrevPtr(root.left, prev);
// Update left pointer
root.left = prev[0];
// Update previous pointer
prev[0] = root;
// Recur for right subtree
fixPrevPtr(root.right, prev);
}
// function to fix right pointers and get the head
static Node fixNextPtr(Node root) {
// Find the rightmost node
Node prev = null;
while (root != null && root.right != null) root = root.right;
// Traverse back using left
// pointers to fix right pointers
while (root != null) {
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
static Node bToDLL(Node root) {
if (root == null) return null;
Node[] prev = new Node[1];
// Fix the left pointers
fixPrevPtr(root, prev);
// Fix the right pointers
// and return the head
return fixNextPtr(root);
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.right;
}
System.out.println();
}
public static void main(String[] args) {
// 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);
}
}
Python
# Python program for in-place
# conversion of Binary Tree to DLL
class Node:
def __init__(self, x):
self.data = x
self.left = None
self.right = None
# function to fix left pointers
def fixPrevPtr(root, prev):
if not root:
return
# Recur for left subtree
fixPrevPtr(root.left, prev)
# Update left pointer
root.left = prev[0]
# Update previous pointer
prev[0] = root
# Recur for right subtree
fixPrevPtr(root.right, prev)
# function to fix right pointers and get the head
def fixNextPtr(root):
# Find the rightmost node
prev = None
while root and 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
def bToDLL(root):
if not root:
return None
prev = [None]
# Fix the left pointers
fixPrevPtr(root, prev)
# Fix the right pointers
# and return the head
return fixNextPtr(root)
def printList(head):
curr = head
while curr:
print(curr.data, end=" ")
curr = curr.right
print()
if __name__ == "__main__":
# Create a hard coded binary tree
# 10
# / \
# 12 15
# / \ /
# 25 30 36
root = Node(10)
root.left = Node(12)
root.right = Node(15)
root.left.left = Node(25)
root.left.right = Node(30)
root.right.left = Node(36)
head = bToDLL(root)
printList(head)
C#
// C# program for in-place
// conversion of Binary Tree to DLL
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node left, right;
public Node(int x) {
data = x;
left = null;
right = null;
}
}
class GfG {
// function to fix left pointers
static void fixPrevPtr(Node root, ref Node prev) {
if (root == null) return;
// Recur for left subtree
fixPrevPtr(root.left, ref prev);
// Update left pointer
root.left = prev;
// Update previous pointer
prev = root;
// Recur for right subtree
fixPrevPtr(root.right, ref prev);
}
// function to fix right pointers and get the head
static Node fixNextPtr(Node root) {
// Find the rightmost node
Node prev = null;
while (root != null && root.right != null) root = root.right;
// Traverse back using left
// pointers to fix right pointers
while (root != null) {
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
static Node bToDLL(Node root) {
if (root == null) return null;
Node prev = null;
// Fix the left pointers
fixPrevPtr(root, ref prev);
// Fix the right pointers
// and return the head
return fixNextPtr(root);
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.data + " ");
curr = curr.right;
}
Console.WriteLine();
}
static void Main(string[] args) {
// 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);
}
}
JavaScript
// JavaScript program for in-place
// conversion of Binary Tree to DLL
class Node {
constructor(x) {
this.data = x;
this.left = null;
this.right = null;
}
}
// function to fix left pointers
function fixPrevPtr(root, prev) {
if (!root) return;
// Recur for left subtree
fixPrevPtr(root.left, prev);
// Update left pointer
root.left = prev.node;
// Update previous pointer
prev.node = root;
// Recur for right subtree
fixPrevPtr(root.right, prev);
}
// function to fix right pointers and get the head
function fixNextPtr(root) {
// Find the rightmost node
let prev = null;
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
function bToDLL(root) {
if (!root) return null;
let prev = { node: null };
// Fix the left pointers
fixPrevPtr(root, prev);
// Fix the right pointers
// and return the head
return fixNextPtr(root);
}
function printList(head) {
let curr = head;
while (curr) {
console.log(curr.data + " ");
curr = curr.right;
}
console.log();
}
// Create a hard coded binary tree
// 10
// / \
// 12 15
// / \ /
// 25 30 36
let 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);
let head = bToDLL(root);
printList(head);
Time Complexity: O(n), where n is the number of nodes in Binary tree.
Auxiliary Space: O(n)
Related Article:
Similar Reads
Convert given Binary Tree to Doubly Linked List in Linear time 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
8 min read
Convert a Binary Tree into Doubly Linked List in spiral fashion Given a binary tree, convert it into a doubly linked list (DLL) where the nodes are arranged in a spiral order. The left pointer of the binary tree node should act as the previous node in the DLL, and the right pointer should act as the next node in the DLL.The solution should not allocate extra mem
10 min read
Convert Binary Tree to Doubly Linked List by keeping track of visited node Given a Binary Tree, The task is to convert it to a Doubly Linked List keeping the same order. The left and right pointers in nodes are to be used as previous and next pointers respectively in converted DLL. The order of nodes in DLL must be the same as in Inorder for the given Binary Tree. The fir
15+ min read
Convert Binary Tree to Doubly Linked List using inorder traversal 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
15+ min read
Convert a Binary Tree to a Circular Doubly Link List Given a Binary Tree, convert it to a Circular Doubly Linked List (In-Place). The left and right pointers in nodes are to be used as previous and next pointers respectively in the converted Circular Linked List.The order of nodes in the List must be the same as in Inorder for the given Binary Tree.Th
15+ min read
Convert Binary Tree to Doubly Linked List using Morris Traversal Given a Binary Tree (BT), convert it to a Doubly Linked List (DLL). The left and right pointers in nodes are to be used as previous and next pointers respectively in converted DLL. The order of nodes in DLL must be the same as in Inorder for the given Binary Tree. The first node of Inorder traversal
12 min read
Print left and right leaf nodes separately in Binary Tree Given a binary tree, the task is to print left and right leaf nodes separately. Examples: Input: 0 / \ 1 2 / \ 3 4 Output: Left Leaf Nodes: 3 Right Leaf Nodes: 4 2 Input: 0 \ 1 \ 2 \ 3 Output: Left Leaf Nodes: None Right Leaf Nodes: 3 Approach: Check if given node is null. If null, then return from
8 min read
Sorted insert in a doubly linked list with head and tail pointers A Doubly linked list is a linked list that consists of a set of sequentially linked records called nodes. Each node contains two fields that are references to the previous and to the next node in the sequence of nodes. The task is to create a doubly linked list by inserting nodes such that list rema
10 min read
Extract Leaves of a Binary Tree in a Doubly Linked List Given a Binary Tree, extract all leaves of it in a Doubly Linked List (DLL). Note that the DLL need to be created in-place. Assume that the node structure of DLL and Binary Tree is same, only the meaning of left and right pointers are different. In DLL, left means previous pointer, and right means n
11 min read
Print all leaf nodes of a binary tree from right to left Given a binary tree, the task is to print all the leaf nodes of the binary tree from right to left. Examples: Input : 1 / \ 2 3 / \ / \ 4 5 6 7 Output : 7 6 5 4 Input : 1 / \ 2 3 / \ \ 4 5 6 / / \ 7 8 9 Output : 9 8 7 4 Recursive Approach: Traverse the tree in Preorder fashion, by first processing t
14 min read