Convert a Binary Tree to a Circular Doubly Linked List

Last Updated : 19 Aug, 2026

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.
  • The first node of Inorder traversal must be the head node of the Circular List.

Examples:

Input: root = [1, 3, 2]

blobid0_1755585384

Output: 3 <-> 1 <-> 2

2056958008

Explanation: The inorder traversal of the binary tree is 3, 1, 2. Therefore, the nodes are connected in the same order to form the Circular Doubly Linked List.

Input: root = [10, 20, 30, 40, 60]

blobid1_1755585451

Output: 40 <-> 20 <-> 60 <-> 10 <-> 30

2056958007

Explanation: The inorder traversal of the binary tree is 40, 20, 60, 10, 30. Hence, the nodes are connected in this order to form the Circular Doubly Linked List.

Try It Yourself
redirect icon

[Expected Approach] Inorder Traversal using Previous Pointer - O(n) Time and O(h) Space

The idea is to perform an inorder traversal of the binary tree and connect each visited node with the previously visited node to form a Doubly Linked List.

Once the traversal is complete, connect the first and last nodes to make it circular.

  • Perform an inorder traversal of the binary tree.
  • Keep track of the first node as head and the previously visited node as prev.
  • For each visited node, if prev is nullptr, set head = root. Otherwise, connect the current node with prev by setting prev->right = root and root->left = prev. Then update prev = root.
  • Continue the inorder traversal by recursively processing the right subtree.
  • After the complete traversal, head points to the first node and prev points to the last node.
  • Connect the last node with the first node by setting prev->right = head and head->left = prev.
  • Return head as the head of the Circular Doubly Linked List.

Consider the following binary tree:

blobid1_1755585451

The inorder traversal of the tree is: 40 -> 20 -> 60 -> 10 -> 30

We process these nodes one by one and connect each node with the previously visited node.

  • For node 40, prev is nullptr, so set head = 40 and prev = 40.
  • For node 20, connect it with 40 and update prev = 20.
  • For node 60, connect it with 20 and update prev = 60.
  • For node 10, connect it with 60 and update prev = 10.
  • For node 30, connect it with 10 and update prev = 30.

After the inorder traversal, the Doubly Linked List is: 40 <-> 20 <-> 60 <-> 10 <-> 30

Here, head points to 40 and prev points to 30. To make the list circular, connect the last node with the first node:

  • prev->right = head
  • head->left = prev

The final Circular Doubly Linked List is:

2056958007
C++
#include <bits/stdc++.h>
using namespace std;

class Node {
public:
    int data;
    Node* left;
    Node* right;

    Node(int x) {
        data = x;
        left = right = nullptr;
    }
};

// Convert Binary Tree to Circular Doubly Linked List.
void inorder(Node* root, Node*& head, Node*& prev) {
    if (root == nullptr)
        return;

    // Recursively traverse the left subtree.
    inorder(root->left, head, prev);

    // Set the first node as head.
    if (prev == nullptr)
        head = root;
    else {
        // Connect the current node with the previous node.
        prev->right = root;
        root->left = prev;
    }

    // Update the previous node.
    prev = root;

    // Recursively traverse the right subtree.
    inorder(root->right, head, prev);
}

// Convert Binary Tree to Circular Doubly Linked List.
Node* bTreeToCList(Node* root) {
    if (root == nullptr)
        return nullptr;

    Node* head = nullptr;
    Node* prev = nullptr;

    inorder(root, head, prev);

    // Connect the last node with the first node.
    prev->right = head;
    head->left = prev;

    return head;
}

// Display the circular doubly linked list.
void display(Node* head) {
    if (head == nullptr)
        return;

    Node* curr = head;

    do {
        cout << curr->data;

        curr = curr->right;

        if (curr != head)
            cout << " <-> ";
    } while (curr != head);

    cout << endl;
}

int main() {

    // Create the binary tree:
    //
    //         10
    //        /  \
    //      20    30
    //     /  \
    //   40    60
    //
    // Inorder: 40 20 60 10 30

    Node* root = new Node(10);
    root->left = new Node(20);
    root->right = new Node(30);
    root->left->left = new Node(40);
    root->left->right = new Node(60);

    Node* head = bTreeToCList(root);

    display(head);

    return 0;
}
Java
class Node {
    int data;
    Node left;
    Node right;

    Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {

    // Convert Binary Tree to Circular Doubly Linked List.
    public static void inorder(Node root, Node[] nodes) {
        if (root == null)
            return;

        // Recursively traverse the left subtree.
        inorder(root.left, nodes);

        // Set the first node as head.
        if (nodes[1] == null)
            nodes[0] = root;
        else {
            // Connect the current node with the previous node.
            nodes[1].right = root;
            root.left = nodes[1];
        }

        // Update the previous node.
        nodes[1] = root;

        // Recursively traverse the right subtree.
        inorder(root.right, nodes);
    }

    // Convert Binary Tree to Circular Doubly Linked List.
    public static Node bTreeToCList(Node root) {
        if (root == null)
            return null;

        Node[] nodes = new Node[2];

        inorder(root, nodes);

        Node head = nodes[0];
        Node prev = nodes[1];

        // Connect the last node with the first node.
        prev.right = head;
        head.left = prev;

        return head;
    }

    // Display the circular doubly linked list.
    public static void display(Node head) {
        if (head == null)
            return;

        Node curr = head;

        do {
            System.out.print(curr.data);

            curr = curr.right;

            if (curr != head)
                System.out.print(" <-> ");
        } while (curr != head);

        System.out.println();
    }

    public static void main(String[] args) {

        // Create the binary tree:
        //
        //         10
        //        /  \
        //      20    30
        //     /  \
        //   40    60
        //
        // Inorder: 40 20 60 10 30

        Node root = new Node(10);
        root.left = new Node(20);
        root.right = new Node(30);
        root.left.left = new Node(40);
        root.left.right = new Node(60);

        Node head = bTreeToCList(root);

        display(head);
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None


# Convert Binary Tree to Circular Doubly Linked List.
def inorder(root, pair):
    if root is None:
        return

    # Recursively traverse the left subtree.
    inorder(root.left, pair)

    # Set the first node as head.
    if pair[1] is None:
        pair[0] = root
    else:
        # Connect the current node with the previous node.
        pair[1].right = root
        root.left = pair[1]

    # Update the previous node.
    pair[1] = root

    # Recursively traverse the right subtree.
    inorder(root.right, pair)


# Convert Binary Tree to Circular Doubly Linked List.
def bTreeToCList(root):
    if root is None:
        return None

    head = None
    prev = None

    pair = [head, prev]
    inorder(root, pair)

    head = pair[0]
    prev = pair[1]

    # Connect the last node with the first node.
    prev.right = head
    head.left = prev

    return head


# Display the circular doubly linked list.
def display(head):
    if head is None:
        return

    curr = head

    while True:
        print(curr.data, end="")

        curr = curr.right

        if curr != head:
            print(" <-> ", end="")
        else:
            break

    print()


if __name__ == "__main__":

    # Create the binary tree:
    #
    #         10
    #        /  \
    #      20    30
    #     /  \
    #   40    60
    #
    # Inorder: 40 20 60 10 30

    root = Node(10)
    root.left = Node(20)
    root.right = Node(30)
    root.left.left = Node(40)
    root.left.right = Node(60)

    head = bTreeToCList(root)

    display(head)
C#
using System;

class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {

    // Convert Binary Tree to Circular Doubly Linked List.
    public static void inorder(Node root, ref Node head, ref Node prev) {
        if (root == null)
            return;

        // Recursively traverse the left subtree.
        inorder(root.left, ref head, ref prev);

        // Set the first node as head.
        if (prev == null)
            head = root;
        else {
            // Connect the current node with the previous node.
            prev.right = root;
            root.left = prev;
        }

        // Update the previous node.
        prev = root;

        // Recursively traverse the right subtree.
        inorder(root.right, ref head, ref prev);
    }

    // Convert Binary Tree to Circular Doubly Linked List.
    public static Node bTreeToCList(Node root) {
        if (root == null)
            return null;

        Node head = null;
        Node prev = null;

        inorder(root, ref head, ref prev);

        // Connect the last node with the first node.
        prev.right = head;
        head.left = prev;

        return head;
    }

    // Display the circular doubly linked list.
    public static void display(Node head) {
        if (head == null)
            return;

        Node curr = head;

        do {
            Console.Write(curr.data);

            curr = curr.right;

            if (curr != head)
                Console.Write(" <-> ");
        } while (curr != head);

        Console.WriteLine();
    }

    public static void Main() {

        // Create the binary tree:
        //
        //         10
        //        /  \
        //      20    30
        //     /  \
        //   40    60
        //
        // Inorder: 40 20 60 10 30

        Node root = new Node(10);
        root.left = new Node(20);
        root.right = new Node(30);
        root.left.left = new Node(40);
        root.left.right = new Node(60);

        Node head = bTreeToCList(root);

        display(head);
    }
}
JavaScript
class Node {
    constructor(x)
    {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

// Convert Binary Tree to Circular Doubly Linked List.
function inorder(root, pair)
{
    if (root === null)
        return;

    // Recursively traverse the left subtree.
    inorder(root.left, pair);

    // Set the first node as head.
    if (pair.prev === null)
        pair.head = root;
    else {
        // Connect the current node with the previous node.
        pair.prev.right = root;
        root.left = pair.prev;
    }

    // Update the previous node.
    pair.prev = root;

    // Recursively traverse the right subtree.
    inorder(root.right, pair);
}

// Convert Binary Tree to Circular Doubly Linked List.
function bTreeToCList(root)
{
    if (root === null)
        return null;

    let head = null;
    let prev = null;

    let pair = {head : head, prev : prev};

    inorder(root, pair);

    head = pair.head;
    prev = pair.prev;

    // Connect the last node with the first node.
    prev.right = head;
    head.left = prev;

    return head;
}

// Display the circular doubly linked list.
function display(head)
{
    if (head === null)
        return;

    let curr = head;

    do {
        process.stdout.write(curr.data.toString());

        curr = curr.right;

        if (curr !== head)
            process.stdout.write(" <-> ");
    } while (curr !== head);

    console.log();
}

// Driver code

// Create the binary tree:
//
//         10
//        /  \
//      20    30
//     /  \
//   40    60
//
// Inorder: 40 20 60 10 30

let root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(60);

let head = bTreeToCList(root);

display(head);

Output
40 <-> 20 <-> 60 <-> 10 <-> 30

[Alternate Approach] Recursion and Concatenation - O(n) Time and O(h) Space

The idea is to recursively convert the left and right subtrees into Circular Doubly Linked Lists and concatenate them with the current node.

  • Recursively convert the left and right subtrees into CDLLs and store their heads in l and r.
  • Make the current node a single-node CDLL by setting its left and right pointers to itself.
  • Concatenate l with the current node, then concatenate the resulting list with r.
  • During concatenation, use the left pointer of each head to access the last node and connect the two circular lists in O(1) time.
  • Return the head of the combined CDLL.

Consider the binary tree:

blobid1_1755585451
  • The inorder traversal is: 40 20 60 10 30
  • The recursion first converts the left subtree: 40 <-> 20 <-> 60
  • The current node 10 is converted into a single-node CDLL: 10
  • Concatenating the left list with 10 gives: 40 <-> 20 <-> 60 <-> 10
  • The right subtree produces: 30
  • Finally, concatenate both lists: 40 <-> 20 <-> 60 <-> 10 <-> 30
  • Thus, the resulting CDLL follows the inorder order of the binary tree.
C++
#include <bits/stdc++.h>
using namespace std;

class Node {
public:
    int data;
    Node* left;
    Node* right;

    Node(int x) {
        data = x;
        left = right = nullptr;
    }
};

// Function to concatenate two lists.
Node* concatenate(Node* l, Node* r) {
    if (l == nullptr)
        return r;

    if (r == nullptr)
        return l;

    // Store the last node of the left list.
    Node* ll = l->left;

    // Store the last node of the right list.
    Node* rl = r->left;

    // Connect the last node of the left list with the first node
    // of the right list.
    ll->right = r;
    r->left = ll;

    // Connect the first node with the last node.
    l->left = rl;
    rl->right = l;

    return l;
}

// Function to convert binary tree into circular doubly linked list.
Node* bTreeToCList(Node* root) {
    if (root == nullptr)
        return nullptr;

    // Recursively convert the left and right subtrees.
    Node* l = bTreeToCList(root->left);
    Node* r = bTreeToCList(root->right);

    // Make the current node a circular linked list of one node.
    root->left = root;
    root->right = root;

    // Concatenate the left list with the current node,
    // then concatenate the result with the right list.
    return concatenate(concatenate(l, root), r);
}

// Display the circular doubly linked list.
void display(Node* head) {
    if (head == nullptr)
        return;

    Node* cur = head;

    do {
        cout << cur->data;

        cur = cur->right;

        if (cur != head)
            cout << " <-> ";
    } while (cur != head);

    cout << endl;
}

int main() {

    // Create the binary tree:
    //
    //         10
    //        /  \
    //      20    30
    //     /  \
    //   40    60
    //
    // Inorder: 40 20 60 10 30

    Node* root = new Node(10);
    root->left = new Node(20);
    root->right = new Node(30);
    root->left->left = new Node(40);
    root->left->right = new Node(60);

    Node* head = bTreeToCList(root);

    display(head);

    return 0;
}
Java
class Node {
    int data;
    Node left, right;

    Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {

    // Function to concatenate two lists.
    static Node concatenate(Node l, Node r) {
        if (l == null)
            return r;

        if (r == null)
            return l;

        // Store the last node of the left list.
        Node ll = l.left;

        // Store the last node of the right list.
        Node rl = r.left;

        // Connect the last node of the left list with the first node
        // of the right list.
        ll.right = r;
        r.left = ll;

        // Connect the first node with the last node.
        l.left = rl;
        rl.right = l;

        return l;
    }

    static Node bTreeToCList(Node root) {
        if (root == null)
            return null;

        // Recursively convert the left and right subtrees.
        Node l = bTreeToCList(root.left);
        Node r = bTreeToCList(root.right);

        // Make the current node a circular linked list of one node.
        root.left = root;
        root.right = root;

        // Concatenate the left list with the current node,
        // then concatenate the result with the right list.
        return concatenate(concatenate(l, root), r);
    }

    // Display the circular doubly linked list.
    static void display(Node head) {
        if (head == null)
            return;

        Node cur = head;

        do {
            System.out.print(cur.data);

            cur = cur.right;

            if (cur != head)
                System.out.print(" <-> ");
        } while (cur != head);

        System.out.println();
    }

    public static void main(String[] args) {

        // Create the binary tree:
        //
        //         10
        //        /  \
        //      20    30
        //     /  \
        //   40    60
        //
        // Inorder: 40 20 60 10 30

        Node root = new Node(10);
        root.left = new Node(20);
        root.right = new Node(30);
        root.left.left = new Node(40);
        root.left.right = new Node(60);

        Node head = bTreeToCList(root);

        display(head);
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None


# Function to concatenate two lists.
def concatenate(l, r):
    if l is None:
        return r

    if r is None:
        return l

    # Store the last node of the left list.
    ll = l.left

    # Store the last node of the right list.
    rl = r.left

    # Connect the last node of the left list with the first node
    # of the right list.
    ll.right = r
    r.left = ll

    # Connect the first node with the last node.
    l.left = rl
    rl.right = l

    return l


def bTreeToCList(root):
    if root is None:
        return None

    # Recursively convert the left and right subtrees.
    l = bTreeToCList(root.left)
    r = bTreeToCList(root.right)

    # Make the current node a circular linked list of one node.
    root.left = root
    root.right = root

    # Concatenate the left list with the current node,
    # then concatenate the result with the right list.
    return concatenate(concatenate(l, root), r)


# Display the circular doubly linked list.
def display(head):
    if head is None:
        return

    cur = head

    while True:
        print(cur.data, end="")

        cur = cur.right

        if cur != head:
            print(" <-> ", end="")
        else:
            break

    print()


if __name__ == "__main__":

    # Create the binary tree:
    #
    #         10
    #        /  \
    #      20    30
    #     /  \
    #   40    60
    #
    # Inorder: 40 20 60 10 30

    root = Node(10)
    root.left = Node(20)
    root.right = Node(30)
    root.left.left = Node(40)
    root.left.right = Node(60)

    head = bTreeToCList(root)

    display(head)
C#
using System;

class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {

    // Function to concatenate two lists.
    public static Node concatenate(Node l, Node r) {
        if (l == null)
            return r;

        if (r == null)
            return l;

        // Store the last node of the left list.
        Node ll = l.left;

        // Store the last node of the right list.
        Node rl = r.left;

        // Connect the last node of the left list with the first node
        // of the right list.
        ll.right = r;
        r.left = ll;

        // Connect the first node with the last node.
        l.left = rl;
        rl.right = l;

        return l;
    }

    public static Node bTreeToCList(Node root) {
        if (root == null)
            return null;

        // Recursively convert the left and right subtrees.
        Node l = bTreeToCList(root.left);
        Node r = bTreeToCList(root.right);

        // Make the current node a circular linked list of one node.
        root.left = root;
        root.right = root;

        // Concatenate the left list with the current node,
        // then concatenate the result with the right list.
        return concatenate(concatenate(l, root), r);
    }

    // Display the circular doubly linked list.
    public static void display(Node head) {
        if (head == null)
            return;

        Node cur = head;

        do {
            Console.Write(cur.data);

            cur = cur.right;

            if (cur != head)
                Console.Write(" <-> ");
        } while (cur != head);

        Console.WriteLine();
    }

    public static void Main() {

        // Create the binary tree:
        //
        //         10
        //        /  \
        //      20    30
        //     /  \
        //   40    60
        //
        // Inorder: 40 20 60 10 30

        Node root = new Node(10);
        root.left = new Node(20);
        root.right = new Node(30);
        root.left.left = new Node(40);
        root.left.right = new Node(60);

        Node head = bTreeToCList(root);
       
        
        display(head);
    }
}
JavaScript
class Node {
    constructor(x)
    {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

// Function to concatenate two lists.
function concatenate(l, r)
{
    if (l === null)
        return r;

    if (r === null)
        return l;

    // Store the last node of the left list.
    let ll = l.left;

    // Store the last node of the right list.
    let rl = r.left;

    // Connect the last node of the left list with the first
    // node of the right list.
    ll.right = r;
    r.left = ll;

    // Connect the first node with the last node.
    l.left = rl;
    rl.right = l;

    return l;
}


function bTreeToCList(root)
{
    if (root === null)
        return null;

    // Recursively convert the left and right subtrees.
    let l = bTreeToCList(root.left);
    let r = bTreeToCList(root.right);

    // Make the current node a circular linked list of one
    // node.
    root.left = root;
    root.right = root;

    // Concatenate the left list with the current node,
    // then concatenate the result with the right list.
    return concatenate(concatenate(l, root), r);
}

// Display the circular doubly linked list.
function display(head)
{
    if (head === null)
        return;

    let cur = head;
    let ans = "";

    do {
        ans += cur.data;

        cur = cur.right;

        if (cur !== head)
            ans += " <-> ";
    } while (cur !== head);

    console.log(ans);
}

// Driver code

// Create the binary tree:
//
//         10
//        /  \
//      20    30
//     /  \
//   40    60
//
// Inorder: 40 20 60 10 30

let root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(60);

let head = bTreeToCList(root);

display(head);

Output
40 <-> 20 <-> 60 <-> 10 <-> 30
Comment