• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Contact Us

All about code

  • Java
    • Spring
      • Spring Boot
      • Spring Cloud
    • Java Miscellaneous Tips
    • Java Design Patterns
  • Data Structures and Algorithms
    • Algorithms
    • Data Structures
  • Thoughts and Stories
  • AI

How to Convert Binary Tree to Doubly Linked List

laptop infographic online business 6087062

Introduction

Converting a binary tree to a doubly linked list (DLL) is a common problem in tree manipulation. The objective is to perform an in-place transformation where the left and right pointers of tree nodes are used as previous and next pointers in the resulting DLL. In this article, we demonstrate how to perform this conversion using an in-order traversal approach in Java.

Problem Statement

Given a binary tree, transform it into a doubly linked list in such a way that the order of nodes in the list reflects the in-order traversal of the tree. Each node’s left pointer functions as the previous link, while the right pointer serves as the next link in the doubly linked list.

Approach

The most intuitive method to achieve this is through in-order traversal, which visits nodes in ascending order for a binary search tree. We keep track of the previously visited node and adjust pointers as we go. The head of the doubly linked list is the leftmost node in the tree.

We use a helper function that recursively traverses the tree in-order, and modifies the left and right pointers of the nodes to stitch them together into a DLL.

Java Implementation

class BinaryTreeToDLL {
	
    static class BinTreeNode {
        int data;
        BinTreeNode left, right;

        BinTreeNode(int data) {
            this.data = data;
            this.left = this.right = null;
        }
    }

    static class Converter {
        BinTreeNode prev = null;
        BinTreeNode head = null;

        public void convertToDLL(BinTreeNode root) {
            if (root == null) return;

            convertToDLL(root.left);

            if (prev == null) {
                head = root;
            } else {
                root.left = prev;
                prev.right = root;
            }
            prev = root;

            convertToDLL(root.right);
        }

        public BinTreeNode getHead() {
            return head;
        }
    }

    public static void printDLL(BinTreeNode head) {
        BinTreeNode current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.right;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        BinTreeNode root = new BinTreeNode(10);
        root.left = new BinTreeNode(6);
        root.right = new BinTreeNode(14);
        root.left.left = new BinTreeNode(4);
        root.left.right = new BinTreeNode(8);
        root.right.left = new BinTreeNode(12);
        root.right.right = new BinTreeNode(16);

        Converter converter = new Converter();
        converter.convertToDLL(root);
        BinTreeNode dllHead = converter.getHead();

        System.out.println("Doubly Linked List:");
        printDLL(dllHead);
    }
}

Example Explanation

Consider the binary tree:

markdownCopiaModifica        10
       /  \
      6    14
     / \   / \
    4   8 12 16

In-order traversal yields: 4, 6, 8, 10, 12, 14, 16
The resulting doubly linked list will have nodes linked in this exact order using the left and right pointers of the original tree nodes.

Conclusion

Converting a binary tree to a doubly linked list is an elegant problem that leverages recursive traversal and pointer manipulation. The in-order approach ensures the doubly linked list maintains a sorted order, especially useful for binary search trees. This transformation is done in-place without using extra data structures, making it efficient in terms of space. Utilizing a queue simplifies the process and enhances efficiency. Gaining familiarity with this approach lays a solid foundation for tackling more advanced tree algorithms in the future.

You can find the example code on Github.

Primary Sidebar

Social

  • Facebook
  • LinkedIn
  • Twitter

Archives

  • May 2026
  • April 2026
  • March 2026
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • September 2023
  • May 2023
  • March 2023
  • January 2023
  • November 2022
  • September 2022
  • August 2022

Recent Posts

  • Better Avoid Spring Boot Lazy-initialization @PostConstruct Trap
  • How To Handle Large Datasets in Spring Boot
  • How to Log HTTP Incoming Requests in Spring Boot
  • How to Reliably Implement Post-Commit Actions in Spring
  • Brief and Easy Explanation of HATEOAS

TAGS

Algorithms Apache POI Backtracking Date Structures Dynamic Programming engineering Graphs Greedy Horror Stories HSSF Java Java New Features Java Principles Java tips real life Sorting Spring Boot Spring Cloud Strings Trees

Footer

Privacy Policy Cookie Policy Terms and Conditions