VOOZH about

URL: https://www.geeksforgeeks.org/dsa/convert-binary-tree-to-doubly-linked-list-using-inorder-traversal/

⇱ Convert Binary Tree to Doubly Linked List using inorder traversal - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Convert Binary Tree to Doubly Linked List using inorder traversal

Last Updated : 23 Jul, 2025

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.


[Naive Approach] Using Recursion - O(n) Time and O(h) Space

The idea is to recursively traverse the binary tree using inorder traversal. At each node, if left subtree exists, find theinorder predecessor, then process the left subtree and link the current node and predecessor. If right subtree exists, then find theinorder successor, process the right subtree and then link the current node and successor node.

Below is the implementation of the above approach:


Output
25 12 30 10 36 15 

Time Complexity: O(n), where n is the number of node in the tree.
Auxiliary Space: O(h), where h is the height of tree.

[Expected Approach] Using Morris Traversal Algorithm - O(n) Time and O(1) Space

The idea is to use Morris traversal algorithmto traverse the binary tree, while maintaining proper linkages between the nodes.

Step by step implementation:

  • Initialize pointers head and tail. head will point to the head node of the resultant dll and tail will point to the last node in dll.
  • Initialize another pointer curr, which will initially point to root node. Start traversing until curr is not NULL
  • If curr.left is null, then add the current node to the list (If head is empty, then make this node as head node) and move curr to curr.right.
  • If curr.left is not null, then find the inorder predecessor of the current node. Let that node be 'pred'. There are two possibilites:
    • If pred.right is equal to null, then create a link between pred and curr, by setting pred.right = curr and set curr = curr.left.
    • If pred->right is equal to curr, then this means we have traversed the left subtree and now we can add the curr node to the list. Then set curr = curr->right.
  • Return the head.

Output
25 12 30 10 36 15 

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

Comment