Convert Binary Search Tree (BST) to Sorted Doubly-Linked List

November 29, 2010 in binary tree, linked list

Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list.

If the problem statement is still not clear to you, below is a pictorial representation of what you need to do:

i) an ordered binary tree (BST) storing numbers from 1 – 5.
ii) original tree converted to a sorted circular doubly-linked list. Its left and right pointers were modified to point to its previous and next node.

I originally read this interesting problem here: The Great Tree-List Recursion Problem.

Think of in-order traversal. How do you ensure that the last element’s right pointer points back to the first element?

A circular doubly-linked list. Think of the left and right pointers in a tree as synonymous to the previous and next pointers in a list.

When I first see this problem, my first thought was in-order traversal. Couldn’t we modify the nodes’ left and right pointers as we do an in-order traversal of the tree? However, we have to beware not to modify the pointers and accessing it at a later time.

As we traverse the tree in-order, we could safely modify a node’s left pointer to point to the previously traversed node as we never use it once we reach a node. We would also need to modify the previously traversed node’s right pointer to point to the current node. Note: The previously traversed node meant here is not its parent node. It is the node’s previous smaller element.

Easy approach, right? But wait, we are still missing two more steps. First, we did not assign the list’s head pointer. Second, the last element’s right pointer does not point to the first element (similar to the first element’s left pointer).

How do we solve this? My approach is pretty easy: Just update the current node’s right pointer to point back to the head and the head’s left pointer to point to current node in each recursive call. As the recursion ends, the list’s head and tail would be automagically updated with the correct pointers. Don’t forget to check for this special case: A list with only one element should have its left and right pointers both pointing back to itself.

A double-linked list with a length of one.

Do you think this approach works? I bet it did! The run time complexity for this solution is O(N) since we are essentially doing a modified in-order traversal. It does have some extra assignments in each recursive call though. But overall I am quite satisfied with this approach because it is intuitive and easy to follow. Besides, we are adapting an existing algorithm (in-order traversal) to solve this problem, isn’t this just neat?

Alternative Solution:
There is an alternative solution which doesn’t use in-order traversal. Instead, it uses a divide and conquer method which is quite neat. I highly recommend you to read the solution (code provided) here.

VN:F [1.9.22_1171]
Rating: 4.3/5 (23 votes cast)
Convert Binary Search Tree (BST) to Sorted Doubly-Linked List, 4.3 out of 5 based on 23 ratings

25 responses to Convert Binary Search Tree (BST) to Sorted Doubly-Linked List

  1. The Alternative Solution's use of recursion just BLEW MY MIND! Amazing solution, using the power of recursion.

    VA:F [1.9.22_1171]
  2. The solutions you provide are really very nice . I have a question and i am not able to solve it .Please help!!
    Question is “Vertical order traversal of a binary tree.”

    VA:F [1.9.22_1171]
  3. J.W. said on May 2, 2011

    Very nice solution, thanks for sharing.

    VA:F [1.9.22_1171]
    • I have two solution
      sol one:
      run time O(N)
      space O(N)

      sol two:
      run time O(N^2)
      space O(1)

      if you are interested in those two solutions, let me no

      VA:F [1.9.22_1171]
  4. HX said on June 9, 2011

    Another solution is to use post-order traverse. The idea is that for each node (considered as a sub tree), we find two pointers that point to its minimum and maximum child. Then we can make the linked list by:
    this_node->left_child->max —-> this_node;
    this_node-> —-> this_node->right_child->min;

    NOTE: I realize that the following code can generate a single link only. Need more thoughts how to modify it to generate double-linked list.

    VA:F [1.9.22_1171]
  5. Hey 1337,

    I’ve tried an alternate solution, making use of a global node which keeps track of the last modified node.
    The code can be found here:

    Please note, this doesn’t create a doubly sorted circular list.

    I wanted your thoughts about this solution and any possible bugs.


    VA:F [1.9.22_1171]
  6. Absolutely awesome!!! saw an alternate solution in stanford site but this is totally awesome

    Keep up the great work mate,cheers!!!

    VA:F [1.9.22_1171]
  7. very impressive!! great job!

    VA:F [1.9.22_1171]
  8. Wouldnt this work as well?

    bsttodll(struct node *rootp, struct node **prevp, struct node **headp){
    if (!rootp) return;
    bsttodll(rootp->left, prevp, headp);
    rootp->left = *prevp;
    if (!*headp)
    *headp = rootp;
    if (*prevp)
    (*prevp)->right = rootp;
    *prevp = rootp;
    bsttodll(rootp->right, prevp, headp);

    VA:F [1.9.22_1171]
  9. I guess it needs the special case, when recursion ends, the head and the final node need to be connected.

    VA:F [1.9.22_1171]
  10. Hey I have written this code..but this does not give the correct output..
    e.g if tree is
    8 20
    3 9 15 25

    output is – 13,20,25
    i.e head pointer n d right subtree..
    I don’t know why for left subtree elements, head is getting updated everytime..
    Please tell me what is the wrong thing in this..

    struct node *doubly(struct node *t,struct node *prev, struct node *head)

    t->left = prev;
    if(prev != NULL)
    struct node *right = t->right;
    prev = t;
    return head;

    treeToDoubly(struct node *t)
    struct node *prev = NULL;
    struct node *head = NULL;
    head = doubly(t,prev,head);

    printf(“List is \n”);

    VA:F [1.9.22_1171]
  11. hi i have a question if (!p) return; it returns what?i am new to programming:)

    VA:F [1.9.22_1171]
  12. my solution is similar, but i think it is very easy to understand.

    class Solution {
    pair convertTreeRecursive(Node *root) {
    if (root == NULL) {
    return pair(NULL, NULL);
    pair left = convertTreeRecursive(root->left);
    pair right = convertTreeRecursive(root->right);

    Node *prevLeft = left.first;
    Node *lastLeft = left.second;
    Node *prevRight = right.first;
    Node *lastRight = right.second;

    if (lastLeft) {
    lastLeft->right = root;
    root->left = lastLeft;

    if (prevRight) {
    prevRight->left = root;
    root->right = prevRight;

    return pair(prevLeft == NULL ? root : prevLeft,
    lastRight == NULL ? root : lastRight);

    Node * convertTree(Node *root) {
    if (root == NULL) {
    return NULL;
    pair ret = convertTreeRecursive(root);
    (ret.first)->left = ret.second;
    (ret.second)->right = ret.first;
    return ret.first;

    VA:F [1.9.22_1171]
  13. VN:F [1.9.22_1171]
  14. VN:F [1.9.22_1171]
  15. VA:F [1.9.22_1171]
  16. Simple inorder will also solve the problem
    v = previously visited node
    h = head of doubly linked list

    VA:F [1.9.22_1171]
  17. The code is precise. But, can u take a small example to explain it.

    VA:F [1.9.22_1171]
  18. why don’t we just use top down approach?

    VN:F [1.9.22_1171]
  19. VA:F [1.9.22_1171]
  20. Hi 1337c0d3r,
    I was asked the same question.But the interviewer said inplace means we can’t use recursion because the stack space will be O(height). Is it possible to do it iteratively with only constant space

    VN:F [1.9.22_1171]
  21. VA:F [1.9.22_1171]
  22. void treeToDll(node *root, node **head, node **tail) {
    if (root == NULL)

    treeToDll(root->left, head, tail);

    /* head [] [] tail (root) */

    root->left = *tail; /* head [] [] tail right = root; /* head [] [] tail (root) */
    *head = root; /* root */

    *tail = root; /* now root is tail of joined list */
    treeToDll(right, head, tail);

    VA:F [1.9.22_1171]

Leave a reply

