{"id":3487,"date":"2021-08-03T09:19:26","date_gmt":"2021-08-03T09:19:26","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=3487"},"modified":"2022-12-13T11:26:43","modified_gmt":"2022-12-13T11:26:43","slug":"convert-a-given-binary-tree-to-doubly-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/","title":{"rendered":"Convert a given Binary Tree to Doubly Linked List"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645528725414-binary%20to%20doubly%20linked%20list-05-04.png\" alt=\"\" \/><\/p>\n<p>This article will discuss in detail how to convert binary tree to doubly linked list. Conversion of binary tree to doubly linked list is an amazing task, which will improve your data structures skills that will help you to achieve your aim. Let\u2019s discuss how to convert convert binary tree to doubly linked list.<\/p>\n<h2>How To Convert Binary Tree To Doubly Linked List<\/h2>\n<p>In this problem, we are given a binary tree. We have to convert the given binary tree to a doubly-linked list.<\/p>\n<p>We have to store the inorder traversal of the given binary tree in the newly created linked list. <\/p>\n<p><strong>Sample Input:<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/4_1-01.png\" alt=\"\" \/><\/p>\n<p><strong>Sample Output:<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/4_2-01.png\" alt=\"\" \/><\/p>\n<h2>Explanation:<\/h2>\n<p>The output doubly linked list contains the in-order traversal of the given binary tree.<\/p>\n<p>We should note that the order of the nodes in the doubly linked list must be the same as the in-order traversal of the binary tree. The first node of the in-order traversal must be the head of the doubly linked list. The left and right pointers will be used as previous and next pointers, respectively. Let us have a glance at the approach.<\/p>\n<h2>Approach#1 To Convert Binary Tree To Doubly Linked List<\/h2>\n<p>In this approach, we will traverse do an inorder traversal of the binary tree. We will add the nodes at the beginning of the newly created linked list, and update the head every time. But there is a problem. Since we will insert at beginning, the order or the linked list that we will recieve, will be reversed.<\/p>\n<p>To correct that, we can do a reverse inorder traversal of the tree, i.e. we will traverse through the right subtree before the left subtree.<\/p>\n<h2>Algorithm To Convert Binary Tree To Doubly Linked List<\/h2>\n<ul>\n<li>Base Case &#8211; If the root is NULL, then simply return.<\/li>\n<li>Recur for the right subtree.<\/li>\n<li>Make the current root\u2019s right point to head, and head\u2019s left pointer to the the current root. By doing this, we are adding the current root to the head , and make the current root the head.<\/li>\n<li>After the above step, recur for the left subtree.<\/li>\n<li>Recursively, the tree will be traversed and the doubly linked list will be created as discussed.<\/li>\n<\/ul>\n<h3>Dry run To Convert Binary Tree To Doubly Linked List<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/4_3-01.png\" alt=\"\" \/><\/p>\n<h2>Code Implementation To Convert Binary Tree To Doubly Linked List<\/h2>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_3495 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_3495 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_3495 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_3495 .wpsm_nav-tabs > li.active > a, #tab_container_3495 .wpsm_nav-tabs > li.active > a:hover, #tab_container_3495 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_3495 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_3495 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_3495 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_3495 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_3495 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_3495 .wpsm_nav-tabs > li > a:hover , #tab_container_3495 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_3495 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_3495 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_3495 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_3495 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_3495 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_3495 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_3495 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3495 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_3495 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3495 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_3495 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_3495\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_3495\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3495_1\" aria-controls=\"tabs_desc_3495_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3495_2\" aria-controls=\"tabs_desc_3495_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3495_3\" aria-controls=\"tabs_desc_3495_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3495_4\" aria-controls=\"tabs_desc_3495_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_3495\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_3495_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include <stdio.h>\r\n#include<stdlib.h>\r\nstruct Node {\r\n    int data;\r\n    struct Node *left;\r\n    struct Node *right;\r\n};\r\n\r\nstruct Node* newNode(int data)\r\n{\r\n    struct Node *node = (struct Node *)malloc(sizeof(struct Node));\r\n    node->data = data;\r\n    node->left = node->right = NULL;\r\n    return node;\r\n}\r\n \r\nvoid BToDLL(struct Node* root,struct Node* head)\r\n{\r\n \r\n    if (root == NULL)\r\n        return;\r\n\r\n    BToDLL(root->right, head);\r\n\r\n    root->right = head;\r\n\r\n    if (head != NULL)\r\n        head->left = root;\r\n\r\n    head = root;\r\n\r\n    BToDLL(root->left, head);\r\n}\r\n\r\nvoid printList(struct Node* head)\r\n{\r\n    printf(\"Extracted Doubly Linked list is:&#92;n\");\r\n    while (head) {\r\n        printf(\"%d \", head->data);\r\n        head = head->right;\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n\r\n    struct Node* root = newNode(11);\r\n    root->left = newNode(12);\r\n    root->right = newNode(15);\r\n    root->left->left = newNode(20);\r\n    root->left->right = newNode(30);\r\n    root->right->left = newNode(36);\r\n\r\n \r\n    struct Node* head = NULL;\r\n    BToDLL(root, head);\r\n \r\n    printList(head);\r\n \r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3495_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n\r\n#include <bits stdc++.h>\r\n\r\nstruct Node {\r\n    int data;\r\n    Node *left, *right;\r\n};\r\n\r\nNode* newNode(int data)\r\n{\r\n    Node* node = new Node;\r\n    node->data = data;\r\n    node->left = node->right = NULL;\r\n    return node;\r\n}\r\n \r\nvoid BToDLL(Node* root, Node*& head)\r\n{\r\n \r\n    if (root == NULL)\r\n        return;\r\n\r\n    BToDLL(root->right, head);\r\n\r\n    root->right = head;\r\n\r\n    if (head != NULL)\r\n        head->left = root;\r\n\r\n    head = root;\r\n\r\n    BToDLL(root->left, head);\r\n}\r\n\r\nvoid printList(Node* head)\r\n{\r\n    printf(\"Extracted Doubly Linked list is:&#92;n\");\r\n    while (head) {\r\n        printf(\"%d \", head->data);\r\n        head = head->right;\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n\r\n    Node* root = newNode(11);\r\n    root->left = newNode(12);\r\n    root->right = newNode(15);\r\n    root->left->left = newNode(20);\r\n    root->left->right = newNode(30);\r\n    root->right->left = newNode(36);\r\n\r\n \r\n    Node* head = NULL;\r\n    BToDLL(root, head);\r\n \r\n    printList(head);\r\n \r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3495_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass Node {\r\n    int data;\r\n    Node left, right;\r\n \r\n    public Node(int data)\r\n    {\r\n        this.data = data;\r\n        left = right = null;\r\n    }\r\n}\r\n \r\npublic class BinaryTree {\r\n\r\n    Node root;\r\n \r\n\r\n    Node head;\r\n \r\n\r\n    void BToDLL(Node root)\r\n    {\r\n\r\n        if (root == null)\r\n            return;\r\n \r\n\r\n        BToDLL(root.right);\r\n \r\n\r\n        root.right = head;\r\n \r\n\r\n        if (head != null)\r\n            head.left = root;\r\n \r\n\r\n        head = root;\r\n \r\n\r\n        BToDLL(root.left);\r\n    }\r\n\r\n    void printList(Node head)\r\n    {\r\n        System.out.println(\"Extracted Doubly Linked List is : \");\r\n        while (head != null) {\r\n            System.out.print(head.data + \" \");\r\n            head = head.right;\r\n        }\r\n    }\r\n\r\n    public static void main(String[] args)\r\n    {\r\n\r\n        BinaryTree tree = new BinaryTree();\r\n        tree.root = new Node(11);\r\n        tree.root.left = new Node(12);\r\n        tree.root.right = new Node(15);\r\n        tree.root.left.left = new Node(20);\r\n        tree.root.left.right = new Node(30);\r\n        tree.root.right.left = new Node(36);\r\n        \r\n        tree.BToDLL(tree.root);\r\n        tree.printList(tree.head);\r\n    }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3495_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass Node:\r\n\tdef __init__(self, data):\r\n\t\tself.right = None\r\n\t\tself.data = data\r\n\t\tself.left = None\r\n\r\n\r\nclass BtoDll:\r\n\tdef __init__(self):\r\n\t\tself.head = None\r\n\t\tself.tail = None\r\n\r\n\tdef convert(self, root):\r\n\t\r\n\t\tif root is None:\r\n\t\t\treturn\r\n\t\t\r\n\t\tself.convert(root.left)\r\n\t\t\r\n\t\tnode = root\r\n\t\tif self.head is None:\r\n\t\t\tself.head = node\r\n\t\telse:\r\n\t\t\tself.tail.right = node\r\n\t\t\tnode.left = self.tail\r\n\t\tself.tail = node\r\n\t\t\r\n\t\tself.convert(root.right)\r\n\t\treturn self.head\r\n\r\n\r\ndef BinaryTree2DoubleLinkedList(root):\r\n\tconverter = BtoDll()\r\n\treturn converter.convert(root)\r\n\r\n\r\ndef print_dll(head):\r\n\twhile head is not None:\r\n\t\tprint(head.data, end=&quot; &quot;)\r\n\t\thead = head.right\r\n\r\n\r\nif __name__ == &quot;__main__&quot;:\r\n\r\n\troot = Node(11)\r\n\troot.left = Node(12)\r\n\troot.right = Node(15)\r\n\troot.left.left = Node(20)\r\n\troot.left.right = Node(30)\r\n\troot.right.left = Node(36)\r\n\t\r\n\thead = BinaryTree2DoubleLinkedList(root)\r\n\tprint(&quot;Extracted Doubly Linked list is:&quot;)\r\n\tprint_dll(head)\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_3495 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_3495 a\"),jQuery(\"#tab-content_3495\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<pre><code>Output :\nExtracted Doubly Linked list is:\n20 12 30 11 36 15<\/code><\/pre>\n<p><strong>Time Complexity To Convert Binary Tree To Doubly Linked List:<\/strong> O(N), as tree traversal is needed.<\/p>\n<p><strong>Space Complexity To Convert Binary Tree To Doubly Linked List:<\/strong> O(N), for the recursion stack and the doubly linked list.<\/p>\n<p>Now, let us have a look at another approach. The time complexities of both the approaches are the same.<\/p>\n<h2>Approach#2 To Convert Binary Tree To Doubly Linked List<\/h2>\n<p>The approach is going to be pretty simple. We will do the in-order traversal of the given tree, and one by one, change the links of each node that we encounter. The doubly linked list will be created In-Place. Let us have a look at the algorithm to get a clearer look. <\/p>\n<h2>Algorithm To Convert Binary Tree To Doubly Linked List<\/h2>\n<ul>\n<li>Base Case &#8211; If the root is NULL, return NULL.<\/li>\n<li>Create a node prev and initialize with NULL.<\/li>\n<li>Recur for the left subtree.<\/li>\n<li>Now, if prev is NULL, then the root will be the head of our doubly linked list.<\/li>\n<li>Else, the left of the root will point to prev and the right of prev will point to the root.<\/li>\n<li>After the above conditional statements, change the value of prev to root.<\/li>\n<li>In the end, recur for the right subtree.<\/li>\n<\/ul>\n<h2>Code Implementation To Convert Binary Tree To Doubly Linked List<\/h2>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_3496 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_3496 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_3496 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_3496 .wpsm_nav-tabs > li.active > a, #tab_container_3496 .wpsm_nav-tabs > li.active > a:hover, #tab_container_3496 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_3496 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_3496 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_3496 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_3496 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_3496 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_3496 .wpsm_nav-tabs > li > a:hover , #tab_container_3496 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_3496 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_3496 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_3496 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_3496 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_3496 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_3496 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_3496 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3496 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_3496 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3496 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_3496 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_3496\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_3496\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3496_1\" aria-controls=\"tabs_desc_3496_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3496_2\" aria-controls=\"tabs_desc_3496_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3496_3\" aria-controls=\"tabs_desc_3496_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_3496\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_3496_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include&lt;stdio.h&gt;\r\n#include&lt;stdlib.h&gt;\r\n\r\nstruct node\r\n{\r\n    int data;\r\n    struct node* left;\r\n    struct node* right;\r\n};\r\n \r\n\/* This is the core function to convert Tree to list. This function follows\r\n  steps 1 and 2 of the above algorithm *\/\r\nstruct node* bintree2listUtil(struct node* root)\r\n{\r\n    \/\/ Base case\r\n    if (root == NULL)\r\n        return root;\r\n \r\n    \/\/ Convert the left subtree and link to root\r\n    if (root-&gt;left != NULL)\r\n    {\r\n        \/\/ Convert the left subtree\r\n        struct node* left = bintree2listUtil(root-&gt;left);\r\n \r\n        \/\/ Find inorder predecessor. After this loop, left\r\n        \/\/ will point to the inorder predecessor\r\n        for (; left-&gt;right!=NULL; left=left-&gt;right);\r\n \r\n        \/\/ Make root as next of the predecessor\r\n        left-&gt;right = root;\r\n \r\n        \/\/ Make predecessor as previous of root\r\n        root-&gt;left = left;\r\n    }\r\n \r\n    \/\/ Convert the right subtree and link to root\r\n    if (root-&gt;right!=NULL)\r\n    {\r\n        \/\/ Convert the right subtree\r\n        struct node* right = bintree2listUtil(root-&gt;right);\r\n \r\n        \/\/ Find inorder successor. After this loop, right\r\n        \/\/ will point to the inorder successor\r\n        for (; right-&gt;left!=NULL; right = right-&gt;left);\r\n \r\n        \/\/ Make root as previous of successor\r\n        right-&gt;left = root;\r\n \r\n        \/\/ Make successor as next of root\r\n        root-&gt;right = right;\r\n    }\r\n \r\n    return root;\r\n}\r\n \r\n\/\/ The main function that first calls bintree2listUtil(), then follows step 3\r\n\/\/  of the above algorithm\r\nstruct node* bintree2list(struct node *root)\r\n{\r\n    \/\/ Base case\r\n    if (root == NULL)\r\n        return root;\r\n \r\n    \/\/ Convert to DLL using bintree2listUtil()\r\n    root = bintree2listUtil(root);\r\n \r\n    \/\/ bintree2listUtil() returns root node of the converted\r\n    \/\/ DLL.  We need pointer to the leftmost node which is\r\n    \/\/ head of the constructed DLL, so move to the leftmost node\r\n    while (root-&gt;left != NULL)\r\n        root = root-&gt;left;\r\n \r\n    return (root);\r\n}\r\n \r\n\/* Helper function that allocates a new node with the\r\n   given data and NULL left and right pointers. *\/\r\nstruct node* newNode(int data)\r\n{\r\n    struct node* new_node = (struct node*)malloc(sizeof(struct node));\r\n    new_node-&gt;data = data;\r\n    new_node-&gt;left = new_node-&gt;right = NULL;\r\n    return (new_node);\r\n}\r\n \r\n\/* Function to print nodes in a given doubly linked list *\/\r\nvoid printList(struct node *node)\r\n{\r\n    while (node!=NULL)\r\n    {\r\n        printf(&quot;%d &quot;, node-&gt;data);\r\n        node = node-&gt;right;\r\n    }\r\n}\r\n \r\n\/* Driver program to test above functions*\/\r\nint main()\r\n{\r\n    \/\/ Let us create the tree shown in above diagram\r\n    struct node *root        = newNode(11);\r\n    root-&gt;left        = newNode(12);\r\n    root-&gt;right       = newNode(15);\r\n    root-&gt;left-&gt;left  = newNode(20);\r\n    root-&gt;left-&gt;right = newNode(30);\r\n    root-&gt;right-&gt;left = newNode(36);\r\n \r\n    \/\/ Convert to DLL\r\n    struct node *head = bintree2list(root);\r\n \r\n    \/\/ Print the converted list\r\n    printList(head);\r\n \r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3496_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;iostream&gt;\r\nusing namespace std;\r\n \r\n\r\nstruct node\r\n{\r\n    int data;\r\n    node* left;\r\n    node* right;\r\n};\r\n \r\n\r\nvoid BinaryTree2DoubleLinkedList(node *root, node **head)\r\n{\r\n    \/\/ Base Case\r\n    if (root == NULL) return;\r\n \r\n    \/\/ Create a prev node\r\n    static node* prev = NULL;\r\n\r\n    \/\/ Recur for left subtree\r\n    BinaryTree2DoubleLinkedList(root-&gt;left, head);\r\n\r\n    \/\/ Make the leftmost element the head ofr DLL\r\n    if (prev == NULL)\r\n        *head = root;\r\n    else\r\n    {\r\n        \/\/ Change links \r\n        root-&gt;left = prev;\r\n        prev-&gt;right = root;\r\n    }\r\n    \/\/ Change the value of prev to root\r\n    prev = root;\r\n \r\n    BinaryTree2DoubleLinkedList(root-&gt;right, head);\r\n}\r\n\r\nnode* newNode(int data)\r\n{\r\n    node* new_node = new node;\r\n    new_node-&gt;data = data;\r\n    new_node-&gt;left = new_node-&gt;right = NULL;\r\n    return (new_node);\r\n}\r\n \r\nvoid printList(node *node)\r\n{\r\n    while (node!=NULL)\r\n    {\r\n        cout &lt;&lt; node-&gt;data &lt;&lt; &quot; &quot;;\r\n        node = node-&gt;right;\r\n    \r\n}\r\n \r\nint main()\r\n{\r\n    \r\n    node *root        = newNode(11);\r\n    root-&gt;left        = newNode(12);\r\n    root-&gt;right       = newNode(15);\r\n    root-&gt;left-&gt;left  = newNode(20);\r\n    root-&gt;left-&gt;right = newNode(30);\r\n    root-&gt;right-&gt;left = newNode(36);\r\n \r\n \r\n    node *head = NULL;\r\n    BinaryTree2DoubleLinkedList(root, &amp;head);\r\n\r\n    printList(head);\r\n \r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3496_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass Node\r\n{\r\n    int data;\r\n    Node left, right;\r\n\r\n    public Node(int data)\r\n    {\r\n        this.data = data;\r\n        left = right = null;\r\n    }\r\n}\r\n\r\npublic class BinaryTree\r\n{\r\n    Node root;\r\n\r\n    Node head;\r\n\r\n    static Node prev = null;\r\n\r\n    void BinaryTree2DoubleLinkedList(Node root)\r\n    {\r\n\r\n        if (root == null)\r\n            return;\r\n\r\n        BinaryTree2DoubleLinkedList(root.left);\r\n\r\n\r\n        if (prev == null)\r\n            head = root;\r\n        else\r\n        {\r\n            root.left = prev;\r\n            prev.right = root;\r\n        }\r\n        prev = root;\r\n\r\n        BinaryTree2DoubleLinkedList(root.right);\r\n    }\r\n\r\n    void printList(Node node)\r\n    {\r\n        while (node != null)\r\n        {\r\n            System.out.print(node.data + &quot; &quot;);\r\n            node = node.right;\r\n        }\r\n    }\r\n\r\n    public static void main(String[] args)\r\n    {\r\n        BinaryTree tree = new BinaryTree();\r\n        tree.root = new Node(11);\r\n        tree.root.left = new Node(12);\r\n        tree.root.right = new Node(15);\r\n        tree.root.left.left = new Node(20);\r\n        tree.root.left.right = new Node(30);\r\n        tree.root.right.left = new Node(36);\r\n\r\n        tree.BinaryTree2DoubleLinkedList(tree.root);\r\n\r\n        tree.printList(tree.head);\r\n\r\n    }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_3496 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_3496 a\"),jQuery(\"#tab-content_3496\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<pre><code>Output:\n20 12 30 11 36 15<\/code><\/pre>\n<p><strong>Time Complexity To Convert Binary Tree To Doubly Linked List:<\/strong> O(N), as a simple in-order traversal is needed.<\/p>\n<p><strong>Space Complexity To Convert Binary Tree To Doubly Linked List:<\/strong> O(N), the space required for recursion stack.<\/p>\n<p>This blog discusses different approaches to convert binary tree to doubly linked list. This is a very important problem when it comes to coding interviews. Solving daily questions for the topics like linked list will definitely help in conquering your dream, you can follow this link <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Linked List<\/a> for practicing more questions of linked list.<\/p>\n<h2>FAQ<\/h2>\n<p><strong>1. How do you convert a given binary tree to a doubly linked list?<\/strong><br \/>\nThis can be achieved by traversing the tree in the in-order manner, that is, left the child -&gt; root -&gt;right node. Traverse left subtree and convert it into the doubly linked list by adding nodes to the end of the list. In this way, the leftmost node will become head of the list.<\/p>\n<p><strong>2. Is a doubly linked list faster?<\/strong><br \/>\nAccessing elements in a Doubly Linked List is more efficient when compared to a Singly Linked List as both forward and backward traversal is possible. The time complexity of inserting or deleting a node at a given position (if the pointer to that position is given) in a singly linked list is O(n).<\/p>\n<p><strong>3. Which companies had recently asked the conversion of binary trees into doubly linked lists?<\/strong><br \/>\nAmazon, Samsung, Indiamart, Flipkart and Zscaler in their recent interviews had asked this question.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article will discuss in detail how to convert binary tree to doubly linked list. Conversion of binary tree to doubly linked list is an amazing task, which will improve your data structures skills that will help you to achieve your aim. Let\u2019s discuss how to convert convert binary tree to doubly linked list. How [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[125],"tags":[],"class_list":["post-3487","post","type-post","status-publish","format-standard","hentry","category-linked-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Convert a given Binary Tree to Doubly Linked List | Linked List | Prepbytes<\/title>\n<meta name=\"description\" content=\"Learn the most efficient way to convert a binary tree to a doubly-linked list. The linked list is one of the most important concepts and data structures to learn while preparing for interviews.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Convert a given Binary Tree to Doubly Linked List | Linked List | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Learn the most efficient way to convert a binary tree to a doubly-linked list. The linked list is one of the most important concepts and data structures to learn while preparing for interviews.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-03T09:19:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-13T11:26:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645528725414-binary%20to%20doubly%20linked%20list-05-04.png\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Convert a given Binary Tree to Doubly Linked List\",\"datePublished\":\"2021-08-03T09:19:26+00:00\",\"dateModified\":\"2022-12-13T11:26:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/\"},\"wordCount\":869,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645528725414-binary%20to%20doubly%20linked%20list-05-04.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/\",\"name\":\"Convert a given Binary Tree to Doubly Linked List | Linked List | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645528725414-binary%20to%20doubly%20linked%20list-05-04.png\",\"datePublished\":\"2021-08-03T09:19:26+00:00\",\"dateModified\":\"2022-12-13T11:26:43+00:00\",\"description\":\"Learn the most efficient way to convert a binary tree to a doubly-linked list. The linked list is one of the most important concepts and data structures to learn while preparing for interviews.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645528725414-binary%20to%20doubly%20linked%20list-05-04.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645528725414-binary%20to%20doubly%20linked%20list-05-04.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linked list articles\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/linked-list\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Convert a given Binary Tree to Doubly Linked List\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Convert a given Binary Tree to Doubly Linked List | Linked List | Prepbytes","description":"Learn the most efficient way to convert a binary tree to a doubly-linked list. The linked list is one of the most important concepts and data structures to learn while preparing for interviews.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Convert a given Binary Tree to Doubly Linked List | Linked List | Prepbytes","og_description":"Learn the most efficient way to convert a binary tree to a doubly-linked list. The linked list is one of the most important concepts and data structures to learn while preparing for interviews.","og_url":"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-08-03T09:19:26+00:00","article_modified_time":"2022-12-13T11:26:43+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645528725414-binary%20to%20doubly%20linked%20list-05-04.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Convert a given Binary Tree to Doubly Linked List","datePublished":"2021-08-03T09:19:26+00:00","dateModified":"2022-12-13T11:26:43+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/"},"wordCount":869,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645528725414-binary%20to%20doubly%20linked%20list-05-04.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/","name":"Convert a given Binary Tree to Doubly Linked List | Linked List | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645528725414-binary%20to%20doubly%20linked%20list-05-04.png","datePublished":"2021-08-03T09:19:26+00:00","dateModified":"2022-12-13T11:26:43+00:00","description":"Learn the most efficient way to convert a binary tree to a doubly-linked list. The linked list is one of the most important concepts and data structures to learn while preparing for interviews.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645528725414-binary%20to%20doubly%20linked%20list-05-04.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645528725414-binary%20to%20doubly%20linked%20list-05-04.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/convert-a-given-binary-tree-to-doubly-linked-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Linked list articles","item":"https:\/\/prepbytes.com\/blog\/category\/linked-list\/"},{"@type":"ListItem","position":3,"name":"Convert a given Binary Tree to Doubly Linked List"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=3487"}],"version-history":[{"count":5,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3487\/revisions"}],"predecessor-version":[{"id":10785,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3487\/revisions\/10785"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=3487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=3487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=3487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}