Introduction

In this article we will discuss about a popular interview question, which is how would you convert a binary tree to a double linked list. Now, a binary tree is a hierachical graph data structure and that does makes binary search tree a part of the question, it just needs to be a binary tree. And a double linked list is a data structure where nodes that hold information are sequentially chained, each node holds a pointer to the next node in the chain.
Binary Tree to Double Linked List

Algorithm

We will choose the most naive approach for solving the problem. We'll do an in order traversal of the binary tree which ends up visiting the nodes in the order: 25 12 30 100, which is the order we want our double linked list to have. We'll keep track of the current node that we're looking at, and the previous node that we've visited. At each step, we'll make the current root node's left pointer point to the previous node, and we'll make the previous node's right pointer point to the current root node. We'll also cover one specific case where our first node becomes the previous node, and we have a head pointer for our linked list point to that node. Here is the C++ algorithm,
  1. #include <iostream>
  2. using namespace std;
  3. struct Node {
  4. int Data;
  5. struct Node* Left;
  6. struct Node* Right;
  7. Node(int data) {
  8. this->Data = data;
  9. this->Left = NULL;
  10. this->Right = NULL;
  11. }
  12. };
  13. void PrintDoubleLinkedList(Node* head) {
  14. // for last nodes
  15. if(head == NULL) {
  16. cout << endl;
  17. return;
  18. }
  19. // print node
  20. cout << head->Data << " ";
  21. // recursively print
  22. PrintDoubleLinkedList(head->Right);
  23. }
  24. void ParseBinaryTree(Node* root, Node*& previous, Node*& header) {
  25. if(root == NULL) {
  26. return;
  27. }
  28. // for left nodes
  29. ParseBinaryTree(root->Left, previous, header);
  30. if(previous == NULL) {
  31. // Set the header & previous node for the first node
  32. header = root;
  33. previous = root;
  34. }
  35. else{
  36. // root node's left pointer point to the previous node
  37. root->Left = previous;
  38. // previous node's right pointer point to the root
  39. previous->Right = root;
  40. // update the previous node
  41. previous = root;
  42. }
  43. // for right nodes
  44. ParseBinaryTree(root->Right, previous, header);
  45. }
  46. int main() {
  47. // initailize a binary tree
  48. Node* root = new Node(100);
  49. root->Left = new Node(12);
  50. root->Left->Left = new Node(25);
  51. root->Left->Right = new Node(30);
  52. // intialize null header & prvious node nodes
  53. Node* header = NULL;
  54. Node* prevNode = NULL;
  55. // parse binary tree to linked list
  56. ParseBinaryTree(root, prevNode, header);
  57. cout << "DOUBLE LINKED LIST : ";
  58. // print the link list
  59. PrintDoubleLinkedList(header);
  60. return 0;
  61. }
The time complexity is O(N), as we visit each node exactly once, and the space complexity is O(1), since we use no extra space.