VOOZH about

URL: https://www.geeksforgeeks.org/dsa/convert-binary-tree-to-doubly-linked-list-by-fixing-left-and-right-pointers/

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 23 Jul, 2025

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.

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:

Comment