{"id":5482,"date":"2021-10-07T04:53:59","date_gmt":"2021-10-07T04:53:59","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=5482"},"modified":"2022-03-10T16:54:12","modified_gmt":"2022-03-10T16:54:12","slug":"flatten-binary-tree-to-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/","title":{"rendered":"Flatten Binary Tree To Linked List"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011403981-Article_181.png\" alt=\"\" \/><\/p>\n<h3>Introduction<\/h3>\n<p>The linked list is one of the most important concepts and data structures to learn while preparing for interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview.<\/p>\n<\/p>\n<h3>Problem Statement<\/h3>\n<p>Given a binary tree, flatten it into a linked list in place. After flattening, the left of each node should point to NULL, and the right should contain the next node in preorder.<\/p>\n<h3>Problem Statement Understanding<\/h3>\n<p>In this problem, we are given a binary tree, and we have to <strong>flatten<\/strong> it and represent it as a linked list. <\/p>\n<p>Now, you all might be confused with the word <strong>flatten<\/strong>, this word simply means that as we know a tree data structure is a non-linear data structure, so what we have to actually do here is to convert this non-linear data structure to a linear data structure (linked list).<\/p>\n<p>Now, let\u2019s try to understand the problem with the help of examples.<\/p>\n<p>Suppose we have a binary tree as:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/example-1-input.png\" alt=\"\" \/><\/p>\n<ul>\n<li>\n<p>Now, according to the problem statement, if we flatten the given binary tree, the flattened version of this binary tree will be:<\/p>\n<\/li>\n<p>![](https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/example-1-output.png)<\/p>\n<li>\n<p>Now, you might wonder how did we get the above-flattened version.<\/p>\n<ul>\n<li>The answer is that if you have read the problem statement carefully, it tells us that we need to flatten the binary tree in such a way that the left child of every node is NULL, and the right child contains the next node in preorder, and here in the flattened version, we can clearly see that all the nodes have their left child as NULL and also right child of each node contains the next node in preorder.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Now, taking another example to make it a bit more clear, If our binary tree is:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/example-2-input.png\" alt=\"\" \/><\/p>\n<ul>\n<li>\n<p>Now, try to draw on your own the flattened representation of the above binary tree.<\/p>\n<\/li>\n<li>\n<p>If not able to draw, it&#8217;s okay; here we will understand step by step how a binary tree is flattened.    <\/p>\n<\/li>\n<\/ul>\n<p>1) We can see that the extreme node to the left in the binary tree is 4, so we will make it as the right of its parent node 2. Also, make sure to preserve the preorder while shifting nodes.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/example-2-input2.png\" alt=\"\" \/><\/p>\n<p>2) Now we will shift all the left child nodes of the parent node 1 to its right following the constraint mentioned in the problem statement, i.e., the left of each node should point to NULL, and the right should contain the next node in preorder.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/example-2-output.png\" alt=\"\" \/><\/p>\n<p>3) Finally, after the above steps, we will have our flattened binary tree in the form of a singly linked list.<\/p>\n<p>Now, I think from the above examples, the problem statement is clear. Let\u2019s see how we can approach it.<\/p>\n<p>Before moving to the approach section, try to think about how you can approach this problem.<\/p>\n<ul>\n<li>If stuck, no problem, we will thoroughly see how we can approach this problem in the next section.<\/li>\n<\/ul>\n<p>Let\u2019s move to the approach section.<\/p>\n<h3>Approach and Algorithm (Brute-Force)<\/h3>\n<p>Now, the approach and algorithm for the brute-force approach will be as follows:<\/p>\n<ul>\n<li>First, declare a stack named <strong>node_stk<\/strong>.<\/li>\n<li>Now we will iterate through the loop satisfying the condition that, either <strong>root<\/strong> node is not NULL or <strong>node_stk<\/strong> is not empty.<\/li>\n<li>Now, check if the <strong>right child<\/strong> of the current root node is not NULL:\n<ul>\n<li>If yes then, push the <strong>right child<\/strong> in <strong>node_stk<\/strong>.<\/li>\n<li>After that shift, the <strong>left node<\/strong> of the current root node to its <strong>right<\/strong>, and point the <strong>left child<\/strong> to NULL.<\/li>\n<\/ul>\n<\/li>\n<li>Now, if <strong>root-&gt;right == NULL<\/strong> and also <strong>node_stk<\/strong> is not empty:\n<ul>\n<li>Then, it means that there was no <strong>left child<\/strong> of the <strong>current root node<\/strong>, so that&#8217;s why we have to make <strong>root-&gt;right<\/strong> point to the <strong>original root&#8217;s right<\/strong> node, which we pushed into the <strong>stack<\/strong> earlier.<\/li>\n<li>We will then <strong>pop<\/strong> the stack.<\/li>\n<\/ul>\n<\/li>\n<li>We will then make <strong>root<\/strong> equal to <strong>root-&gt;right<\/strong>, and the above process will repeat until we flatten our binary tree completely.<\/li>\n<\/ul>\n<h3>Code Implementation<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_5483 {\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_5483 .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_5483 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_5483 .wpsm_nav-tabs > li.active > a, #tab_container_5483 .wpsm_nav-tabs > li.active > a:hover, #tab_container_5483 .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_5483 .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_5483 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_5483 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_5483 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_5483 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_5483 .wpsm_nav-tabs > li > a:hover , #tab_container_5483 .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_5483 .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_5483 .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_5483 .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_5483 .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_5483 .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_5483 .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_5483 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5483 .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_5483 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5483 .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_5483 .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_5483\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_5483\">\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_5483_1\" aria-controls=\"tabs_desc_5483_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_5483_2\" aria-controls=\"tabs_desc_5483_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>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\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_5483_3\" aria-controls=\"tabs_desc_5483_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_5483\">\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_5483_1\">\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 <iostream>\r\n#include <stack>\r\nusing namespace std;\r\n \r\nstruct Node {\r\n    int key;\r\n    Node *left, *right;\r\n};\r\n \r\n\/* utility that allocates a new Node\r\n   with the given key  *\/\r\nNode* newNode(int key)\r\n{\r\n    Node* node = new Node;\r\n    node->key = key;\r\n    node->left = node->right = NULL;\r\n    return (node);\r\n}\r\n \r\n\/\/ To find the inorder traversal\r\nvoid inorder(struct Node* root)\r\n{\r\n    \/\/ base condition\r\n    if (root == NULL)\r\n        return;\r\n    inorder(root->left);\r\n    cout << root->key << \" \";\r\n    inorder(root->right);\r\n}\r\n \r\n\/\/ Function to convert binary tree into\r\n\/\/ linked list by altering the right node\r\n\/\/ and making left node point to NULL\r\nNode* solution(Node* A)\r\n{\r\n \r\n    \/\/ Declare a stack\r\n    stack<Node*> st;\r\n    Node* ans = A;\r\n \r\n    \/\/ Iterate till the stack is not empty\r\n    \/\/ and till root is Null\r\n    while (A != NULL || st.size() != 0) {\r\n \r\n        \/\/ Check for NULL\r\n        if (A->right != NULL) {\r\n            st.push(A->right);\r\n        }\r\n \r\n        \/\/ Make the Right Left and\r\n        \/\/ left NULL\r\n        A->right = A->left;\r\n        A->left = NULL;\r\n \r\n        \/\/ Check for NULL\r\n        if (A->right == NULL && st.size() != 0) {\r\n            A->right = st.top();\r\n            st.pop();\r\n        }\r\n \r\n        \/\/ Iterate\r\n        A = A->right;\r\n    }\r\n    return ans;\r\n}\r\n \r\n\/\/ Driver Code\r\nint main()\r\n{\r\n    \/*    1\r\n        \/   &#92;\r\n       2     5\r\n      \/ &#92;     &#92;\r\n     3   4     6 *\/\r\n \r\n    \/\/ Build the tree\r\n    Node* root = newNode(1);\r\n    root->left = newNode(2);\r\n    root->right = newNode(5);\r\n    root->left->left = newNode(3);\r\n    root->left->right = newNode(4);\r\n    root->right->right = newNode(6);\r\n \r\n    \/\/ Call the function to\r\n    \/\/ flatten the tree\r\n    root = solution(root);\r\n \r\n    cout << \"The Inorder traversal after \"\r\n            \"flattening binary tree \";\r\n \r\n    \/\/ call the function to print\r\n    \/\/ inorder after flattening\r\n    inorder(root);\r\n    return 0;\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_5483_2\">\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\t\r\n\tdef __init__(self):\r\n\t\t\r\n\t\tself.key = 0\r\n\t\tself.left = None\r\n\t\tself.right = None\r\n\r\n# Utility that allocates a new Node\r\n# with the given key\r\ndef newNode(key):\r\n\t\r\n\tnode = Node()\r\n\tnode.key = key\r\n\tnode.left = node.right = None\r\n\treturn (node)\r\n\r\n# Function to convert binary tree into linked list\r\ndef flatten(root):\r\n\r\n\tnode_stk = []\r\n\tans = root\r\n\t# Iterate till the stack is not empty and till root is Null\r\n\twhile (root or len(node_stk)):\r\n\t\t# Check for NULL\r\n\t\tif (root.right):\r\n\t\t\tnode_stk.append(root.right)\r\n\t\t\r\n\t\t# Make the Right node  Left and left  node NULL\r\n\t\troot.right = root.left\r\n\t\troot.left = None\r\n\t\t\r\n\t\t# Check for NULL condition\r\n\t\tif (root.right == None and len(node_stk) != 0):\r\n\t\t\troot.right = node_stk[-1]\r\n\t\t\tnode_stk.pop()\r\n\t\t\r\n\t\t# Iterate to the right child\r\n\t\troot= root.right\r\n\t\r\n\treturn ans\r\n\r\n# To find the inorder traversal\r\ndef inorder(root):\r\n\r\n\t# Base condition\r\n\tif (root == None):\r\n\t\treturn\r\n\t\r\n\tinorder(root.left)\r\n\tprint(root.key, end = ' ')\r\n\tinorder(root.right)\r\n\r\n# Driver Code\r\nif __name__=='__main__':\r\n\t\r\n\troot = newNode(1)\r\n\troot.left = newNode(2)\r\n\troot.right = newNode(3)\r\n\troot.left.right = newNode(4)\r\n\troot.left.right.right = newNode(5)\r\n\r\n\tprint(\"The Inorder traversal before flattening binary tree\",end=\" \")\r\n\tinorder(root)\r\n\tprint()\r\n\tflatten(root)\r\n\r\n\tprint(\"The Inorder traversal after \"\r\n\t\t\"flattening binary tree \",\r\n\t\tend = '')\r\n\tinorder(root)\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_5483_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\tint data;\r\n\tNode left, right;\r\n\tNode(int key)\r\n\t{\r\n\t\tdata = key;\r\n\t\tleft = right = null;\r\n\t}\r\n}\r\nclass Flatten \r\n{\r\n\r\n\tNode root;\r\n\tpublic void flatten(Node node)\r\n\t{\r\n\t\t\/\/ Base case - return if root is NULL\r\n\t\tif (node == null)\r\n\t\t\treturn;\r\n\t\t\/\/ Or if it is a leaf node\r\n\t\tif (node.left == null &amp;&amp; node.right == null)\r\n\t\t\treturn;\r\n\t\t\/\/ If root.left children exists then we have to make\r\n\t\t\/\/ it node.right (where node is root)\r\n\t\tif (node.left != null) {\r\n\t\t\t\/\/ Move left recursively\r\n\t\t\tflatten(node.left);\r\n\t\t\t\/\/ Store the node.right in Node named tempNode\r\n\t\t\tNode tempNode = node.right;\r\n\t\t\tnode.right = node.left;\r\n\t\t\tnode.left = null;\r\n\t\t\t\/\/ Find the position to insert the stored value\r\n\t\t\tNode curr = node.right;\r\n\t\t\twhile (curr.right != null)\r\n\t\t\t\tcurr = curr.right;\r\n\t\t\t\/\/ Insert the stored value\r\n\t\t\tcurr.right = tempNode;\r\n\t\t}\r\n\t\t\/\/ Now call the same function for node.right\r\n\t\tflatten(node.right);\r\n\t}\r\n\tpublic void inOrder(Node node)\r\n\t{\r\n\t\tif (node == null)\r\n\t\t\treturn;\r\n\t\tinOrder(node.left);\r\n\t\tSystem.out.print(node.data + &quot; &quot;);\r\n\t\tinOrder(node.right);\r\n\t}\r\n\tpublic static void main(String[] args)\r\n\t{\r\n\t\tFlatten tree=new Flatten();\r\n\t\ttree.root = new Node(1);\r\n\t\ttree.root.left = new Node(2);\r\n\t\ttree.root.right = new Node(5);\r\n\t\ttree.root.left.left = new Node(3);\r\n\t\ttree.root.left.right = new Node(4);\r\n\t\ttree.root.right.right = new Node(6);\r\n\r\n\t\tSystem.out.println(&quot;The Inorder traversal after flattening binary tree &quot;);\r\n\t\ttree.flatten(tree.root);\r\n\t\ttree.inOrder(tree.root);\r\n\t}\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_5483 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_5483 a\"),jQuery(\"#tab-content_5483\"));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<h4>Output<\/h4>\n<p>The Inorder traversal before flattening binary tree 2 4 5 1 3<br \/>\nThe Inorder traversal after flattening binary tree 1 2 4 5 3 <\/p>\n<p><strong>Time Complexity:<\/strong> O(n)<br \/>\n<strong>Space Complexity:<\/strong> O(n)<\/p>\n<p>The above algorithm will work fine, but the main issue is that it takes O(n) extra space. Can we solve the above problem in O(1) extra space?<\/p>\n<ul>\n<li>Okay! If you can\u2019t think of an approach, let me give you a hint from the previous solution. <\/li>\n<li>In the previous solution, we have used a stack to solve the problem; what if we use a pointer to temporarily store the right node and then keep putting it after left node, if it exists and this process would go on recursively till all the left child nodes are not NULL.<\/li>\n<\/ul>\n<p>Still confused? Don\u2019t worry, let\u2019s get this recursive approach more clear by discussing it further.<\/p>\n<h3>Approach (Pointer Method)<\/h3>\n<p>Our approach will be:<\/p>\n<ul>\n<li>Since it will be a recursive approach, so we will need a base case that will terminate the function, and our base case will be:\n<ul>\n<li>If <strong>root<\/strong> is NULL or if it is a <strong>leaf node<\/strong>, then return.<\/li>\n<\/ul>\n<\/li>\n<li>Get a pointer that will point to the <strong>right child<\/strong> of the <strong>current parent node<\/strong>, and then replace the <strong>right child<\/strong> of the <strong>parent node<\/strong> with the <strong>left child node<\/strong>.\n<ul>\n<li>Now, once the node is replaced, we will traverse to the <strong>rightmost leaf node<\/strong> of the tree and then will attach the <strong>last stored right node<\/strong> to the right of <strong>rightmost leaf node<\/strong>, because we have to preserve the order of nodes (preorder) which is given to us.<\/li>\n<\/ul>\n<\/li>\n<li>And this process will be repeated till all the <strong>left child nodes<\/strong> are not NULL.<\/li>\n<\/ul>\n<p>Now, to get a clear understanding of how this approach works, let\u2019s have a look at its algorithm:<\/p>\n<h3>Algorithm (Pointer Method)<\/h3>\n<p>1) <strong>Base Case:<\/strong> if the <strong>root<\/strong> is NULL or the <strong>root<\/strong> is a <strong>leaf node<\/strong>, we will end the recursive function and return, else we will move on to step 2.<br \/>\n2) Now, we will check if the root has a <strong>left child<\/strong>.<\/p>\n<ul>\n<li>Yes, if the root has a <strong>left child<\/strong>, then store the <strong>right child<\/strong> of the <strong>root node<\/strong> in a pointer named <strong>right_child<\/strong>.<\/li>\n<li>After that, shift the <strong>left child<\/strong> of the <strong>root<\/strong> to the <strong>right child<\/strong> and make the <strong>left child<\/strong> NULL:\n<ul>\n<li>root-&gt;right = root-&gt;left;<\/li>\n<li>root-&gt;left = NULL;<\/li>\n<\/ul>\n<\/li>\n<li>And then, after shifting, we will traverse to the <strong>rightmost leaf node<\/strong>, and after reaching it, we will make its <strong>right pointer<\/strong> point to the node pointed by <strong>right_child<\/strong>.<br \/>\n3) Now, if there is no <strong>left child node<\/strong>, call the function recursively for the <strong>right child node<\/strong> and repeat the process.<\/li>\n<\/ul>\n<h3>Dry Run<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_1-36.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_2-35.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_3-21.png\" alt=\"\" \/><\/p>\n<h3>Code Implementation<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_5485 {\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_5485 .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_5485 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_5485 .wpsm_nav-tabs > li.active > a, #tab_container_5485 .wpsm_nav-tabs > li.active > a:hover, #tab_container_5485 .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_5485 .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_5485 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_5485 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_5485 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_5485 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_5485 .wpsm_nav-tabs > li > a:hover , #tab_container_5485 .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_5485 .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_5485 .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_5485 .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_5485 .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_5485 .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_5485 .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_5485 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5485 .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_5485 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5485 .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_5485 .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_5485\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_5485\">\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_5485_1\" aria-controls=\"tabs_desc_5485_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_5485_2\" aria-controls=\"tabs_desc_5485_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_5485_3\" aria-controls=\"tabs_desc_5485_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_5485_4\" aria-controls=\"tabs_desc_5485_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_5485\">\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_5485_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    int key;\r\n    struct Node *left;\r\n    struct Node *right;\r\n};\r\n \r\n\/* utility that allocates a new Node\r\n   with the given key  *\/\r\nstruct Node* newNode(int key)\r\n{\r\n    struct Node* node = malloc(sizeof(struct Node*));\r\n    node-&gt;key = key;\r\n    node-&gt;left = node-&gt;right = NULL;\r\n    return (node);\r\n}\r\n \r\n\/\/ Function to convert binary tree into\r\n\/\/ linked list by altering the right node\r\n\/\/ and making left node point to NULL\r\nvoid flatten(struct Node* root)\r\n{\r\n    \/\/ base condition- return if root is NULL\r\n    \/\/ or if it is a leaf node\r\n    if (root == NULL || root-&gt;left == NULL &amp;&amp;\r\n                        root-&gt;right == NULL) {\r\n        return;\r\n    }\r\n \r\n    \/\/ if root-&gt;left exists then we have\r\n    \/\/ to make it root-&gt;right\r\n    if (root-&gt;left != NULL) {\r\n \r\n        \/\/ move left recursively\r\n        flatten(root-&gt;left);\r\n    \r\n        \/\/ store the node root-&gt;right\r\n        struct Node* tmpRight = root-&gt;right;\r\n        root-&gt;right = root-&gt;left;\r\n        root-&gt;left = NULL;\r\n \r\n        \/\/ find the position to insert\r\n        \/\/ the stored value  \r\n        struct Node* t = root-&gt;right;\r\n        while (t-&gt;right != NULL) {\r\n            t = t-&gt;right;\r\n        }\r\n \r\n        \/\/ insert the stored value\r\n        t-&gt;right = tmpRight;\r\n    }\r\n \r\n    \/\/ now call the same function\r\n    \/\/ for root-&gt;right\r\n    flatten(root-&gt;right);\r\n}\r\n \r\n\/\/ To find the inorder traversal\r\nvoid inorder(struct Node* root)\r\n{\r\n    \/\/ base condition\r\n    if (root == NULL)\r\n        return;\r\n    inorder(root-&gt;left);\r\n    printf(&quot;%d &quot;,root-&gt;key);\r\n    inorder(root-&gt;right);\r\n}\r\n \r\n\/* Driver program to test above functions*\/\r\nint main()\r\n{\r\n    \/*    1\r\n        \/   &#92;\r\n       2     5\r\n      \/ &#92;     &#92;\r\n     3   4     6 *\/\r\n    struct Node* root = newNode(1);\r\n    root-&gt;left = newNode(2);\r\n    root-&gt;right = newNode(5);\r\n    root-&gt;left-&gt;left = newNode(3);\r\n    root-&gt;left-&gt;right = newNode(4);\r\n    root-&gt;right-&gt;right = newNode(6);\r\n \r\n    flatten(root);\r\n \r\n    printf( &quot;The Inorder traversal after &quot;\r\n            &quot;flattening binary tree &quot;);\r\n    inorder(root);\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_5485_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;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n \r\nstruct Node {\r\n    int key;\r\n    Node *left, *right;\r\n};\r\n \r\n\/\/ utility that allocates a new Node with the given key\r\nNode* newNode(int key)\r\n{\r\n    Node* node = new Node;\r\n    node-&gt;key = key;\r\n    node-&gt;left = node-&gt;right = NULL;\r\n    return (node);\r\n}\r\n \r\n\/\/ Function to convert binary tree into linked list by\r\n\/\/ altering the right node and making left node point to\r\n\/\/ NULL\r\nvoid flatten(struct Node* root)\r\n{\r\n    \/\/ base condition- return if root is NULL or if it is a\r\n    \/\/ leaf node\r\n    if (root == NULL || root-&gt;left == NULL &amp;&amp; root-&gt;right == NULL)\r\n        return;\r\n    \/\/ if root-&gt;left exists then we have to make it\r\n    \/\/ root-&gt;right\r\n    if (root-&gt;left != NULL) {\r\n        \/\/ move left recursively\r\n        flatten(root-&gt;left);\r\n        \/\/ store the node root-&gt;right\r\n        struct Node* tmpRight = root-&gt;right;\r\n        root-&gt;right = root-&gt;left;\r\n        root-&gt;left = NULL;\r\n        \/\/ find the position to insert the stored value\r\n        struct Node* t = root-&gt;right;\r\n        while (t-&gt;right != NULL)\r\n            t = t-&gt;right;\r\n        \/\/ insert the stored value\r\n        t-&gt;right = tmpRight;\r\n    }\r\n    \/\/ now call the same function for root-&gt;right\r\n    flatten(root-&gt;right);\r\n}\r\n \r\n\/\/ To find the inorder traversal\r\nvoid inorder(struct Node* root)\r\n{\r\n    \/\/ base condition\r\n    if (root == NULL)\r\n        return;\r\n    inorder(root-&gt;left);\r\n    cout &lt;&lt; root-&gt;key &lt;&lt; &quot; &quot;;\r\n    inorder(root-&gt;right);\r\n}\r\n \r\n\/* Driver program to test above functions*\/\r\nint main()\r\n{\r\n    \/*    1\r\n        \/   &#92;\r\n       2     5\r\n      \/ &#92;     &#92;\r\n     3   4     6 *\/\r\n    Node* root = newNode(1);\r\n    root-&gt;left = newNode(2);\r\n    root-&gt;right = newNode(5);\r\n    root-&gt;left-&gt;left = newNode(3);\r\n    root-&gt;left-&gt;right = newNode(4);\r\n    root-&gt;right-&gt;right = newNode(6);\r\n    flatten(root);\r\n    cout &lt;&lt; &quot;The Inorder traversal after flattening binary tree &quot;;\r\n    inorder(root);\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_5485_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\tint data;\r\n\tNode left, right;\r\n\r\n\tNode(int key)\r\n\t{\r\n\t\tdata = key;\r\n\t\tleft = right = null;\r\n\t}\r\n}\r\n\r\nclass Flatten\r\n{\r\n\tNode root;\r\n    \r\n    public void flatten(Node node)\r\n    {\r\n\t    if (node == null)\r\n\t\t    return;\r\n\r\n\t    if (node.left == null &amp;&amp;\r\n\t        node.right == null)\r\n\t\t    return;\r\n\r\n\t    if (node.left != null)\r\n\t    {\r\n\t\t    flatten(node.left);\r\n\t\t    Node tempNode = node.right;\r\n\t\t    node.right = node.left;\r\n\t\t    node.left = null;\r\n\r\n\t\t    Node curr = node.right;\r\n\t\t    while (curr.right != null)\r\n\t\t    {\r\n\t\t\t    curr = curr.right;\r\n\t\t    }\r\n\t\t    curr.right = tempNode;\r\n\t    }\r\n\t    flatten(node.right);\r\n    }\r\n    public void inOrder(Node node)\r\n    {\r\n        if (node == null)\r\n\t\t    return;\r\n\r\n\t    inOrder(node.left);\r\n\t    System.out.print(node.data + &quot; &quot;);\r\n\t    inOrder(node.right);\r\n    }\r\n    public static void main(String[] args)\r\n    {\r\n\t    Flatten tree = new Flatten();\r\n\r\n\t    tree.root = new Node(1);\r\n\t    tree.root.left = new Node(2);\r\n\t    tree.root.right = new Node(5);\r\n\t    tree.root.left.left = new Node(3);\r\n\t    tree.root.left.right = new Node(4);\r\n\t    tree.root.right.right = new Node(6);\r\n\r\n\t    System.out.println(&quot;The Inorder traversal after &quot; +&quot;flattening binary tree &quot;);\r\n\t\t\t\t\t\t\r\n\t    tree.flatten(tree.root);\r\n\t    tree.inOrder(tree.root);\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\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_5485_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\t\r\n\tdef __init__(self):\r\n\t\t\r\n\t\tself.key = 0\r\n\t\tself.left = None\r\n\t\tself.right = None\r\n\r\n# Utility that allocates a new Node\r\n# with the given key\r\ndef newNode(key):\r\n\t\r\n\tnode = Node()\r\n\tnode.key = key\r\n\tnode.left = node.right = None\r\n\treturn (node)\r\n\r\n# Function to convert binary tree into linked list\r\ndef flatten(root):\r\n\r\n\t# Base condition- return if root is None\r\n\t# or if it is a leaf node\r\n\tif (root == None or root.left == None and\r\n\t\t\t\t\t\troot.right == None):\r\n\t\treturn\r\n\t\r\n\t# If root.left exists then we have\r\n\t# to make it root.right\r\n\tif (root.left != None):\r\n\r\n\t\t# Move left recursively\r\n\t\tflatten(root.left)\r\n\t\r\n\t\t# Store the node root.right\r\n\t\ttmpRight = root.right\r\n\t\troot.right = root.left\r\n\t\troot.left = None\r\n\r\n\t\t# Find the position to insert\r\n\t\t# the stored value\r\n\t\tt = root.right\r\n\t\twhile (t.right != None):\r\n\t\t\tt = t.right\r\n\r\n\t\t# Insert the stored value\r\n\t\tt.right = tmpRight\r\n\r\n\t# Now call the same function\r\n\t# for root.right\r\n\tflatten(root.right)\r\n\r\n# To find the inorder traversal\r\ndef inorder(root):\r\n\r\n\t# Base condition\r\n\tif (root == None):\r\n\t\treturn\r\n\t\r\n\tinorder(root.left)\r\n\tprint(root.key, end = ' ')\r\n\tinorder(root.right)\r\n\r\n# Driver Code\r\nif __name__=='__main__':\r\n\t\r\n\troot = newNode(1)\r\n\troot.left = newNode(2)\r\n\troot.right = newNode(3)\r\n\troot.left.right = newNode(4)\r\n\troot.left.right.right = newNode(5)\r\n\r\n\tprint(&quot;The Inorder traversal before flattening binary tree&quot;,end=&quot; &quot;)\r\n\tinorder(root)\r\n\tprint()\r\n\tflatten(root)\r\n\r\n\tprint(&quot;The Inorder traversal after &quot;\r\n\t\t&quot;flattening binary tree &quot;,\r\n\t\tend = '')\r\n\tinorder(root)\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_5485 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_5485 a\"),jQuery(\"#tab-content_5485\"));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<h4>Output<\/h4>\n<p>The Inorder traversal before flattening binary tree 2 4 5 1 3<br \/>\nThe Inorder traversal after flattening binary tree 1 2 4 5 3 <\/p>\n<p><strong>Time Complexity:<\/strong> The time complexity of this algorithm will be O(n), where n is the number of nodes in the binary tree<\/p>\n<p>[forminator_quiz id=&#8221;5484&#8243;]<\/p>\n<p>So, in this article, we have tried to explain the most efficient approach to flatten a binary tree to a linked list. This is an important question when it comes to coding interviews. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Linked List<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction The linked list is one of the most important concepts and data structures to learn while preparing for interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview. Problem Statement Given a binary tree, flatten it into a linked list in place. After flattening, the left [&hellip;]<\/p>\n","protected":false},"author":3,"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-5482","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>Flatten Binary Tree To Linked List<\/title>\n<meta name=\"description\" content=\"In this article, we have tried to explain the most efficient approach to flatten a binary tree to a linked list.\" \/>\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\/flatten-binary-tree-to-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Flatten Binary Tree To Linked List\" \/>\n<meta property=\"og:description\" content=\"In this article, we have tried to explain the most efficient approach to flatten a binary tree to a linked list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-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-10-07T04:53:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-10T16:54:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011403981-Article_181.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=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"Flatten Binary Tree To Linked List\",\"datePublished\":\"2021-10-07T04:53:59+00:00\",\"dateModified\":\"2022-03-10T16:54:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/\"},\"wordCount\":1236,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011403981-Article_181.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/\",\"name\":\"Flatten Binary Tree To Linked List\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011403981-Article_181.png\",\"datePublished\":\"2021-10-07T04:53:59+00:00\",\"dateModified\":\"2022-03-10T16:54:12+00:00\",\"description\":\"In this article, we have tried to explain the most efficient approach to flatten a binary tree to a linked list.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011403981-Article_181.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011403981-Article_181.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-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\":\"Flatten Binary Tree To 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\/39fcf072e04987f16796546f2ca83c2e\",\"name\":\"PrepBytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"caption\":\"PrepBytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Flatten Binary Tree To Linked List","description":"In this article, we have tried to explain the most efficient approach to flatten a binary tree to a linked list.","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\/flatten-binary-tree-to-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Flatten Binary Tree To Linked List","og_description":"In this article, we have tried to explain the most efficient approach to flatten a binary tree to a linked list.","og_url":"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-10-07T04:53:59+00:00","article_modified_time":"2022-03-10T16:54:12+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011403981-Article_181.png","type":"","width":"","height":""}],"author":"PrepBytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"PrepBytes","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"Flatten Binary Tree To Linked List","datePublished":"2021-10-07T04:53:59+00:00","dateModified":"2022-03-10T16:54:12+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/"},"wordCount":1236,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011403981-Article_181.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/","name":"Flatten Binary Tree To Linked List","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011403981-Article_181.png","datePublished":"2021-10-07T04:53:59+00:00","dateModified":"2022-03-10T16:54:12+00:00","description":"In this article, we have tried to explain the most efficient approach to flatten a binary tree to a linked list.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011403981-Article_181.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011403981-Article_181.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/flatten-binary-tree-to-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":"Flatten Binary Tree To 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\/39fcf072e04987f16796546f2ca83c2e","name":"PrepBytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","caption":"PrepBytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5482","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=5482"}],"version-history":[{"count":6,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5482\/revisions"}],"predecessor-version":[{"id":7910,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5482\/revisions\/7910"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=5482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=5482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=5482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}