# 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 converted Circular Linked List.
* The order of nodes in List must be same as Inorder of the given Binary Tree.
* The first node of Inorder traversal must be head node of the Circular List.

**Example:**<br>

![](https://media.geeksforgeeks.org/wp-content/cdn-uploads/tree-to-list.png)

```java
class Solution {
    Node last;

    Node bToDLL(Node root) {
        if (root == null)
            return root;
        Node leftEnd = bToDLL(root.left);
        if (last != null) {
            last.right = root;
            root.left = last;
        }
        last = root;
        Node rightEnd = bToDLL(root.right);
        root.right = rightEnd;
        if (rightEnd != null)
            rightEnd.left = root;
        return leftEnd == null ? root : leftEnd;
    }

    Node bTreeToClist(Node root) {
        if (root == null)
            return root;
        Node head = bToDLL(root);
        head.left = last;
        last.right = head;
        return head;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/trees/binary-tree-to-a-circular-doubly-link-list.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
