<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":507,"date":"2021-07-13T17:29:55","date_gmt":"2021-07-13T17:29:55","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=507"},"modified":"2021-09-13T06:25:01","modified_gmt":"2021-09-13T06:25:01","slug":"inorder-tree-traversal-without-recursion-and-without-stack","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/inorder-tree-traversal-without-recursion-and-without-stack\/","title":{"rendered":"Inorder Tree Traversal without recursion and without stack ?"},"content":{"rendered":"<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"inorder-tree-traversal-without-recursion-and-without-stack\" class=\"color-pink\" style=\"text-align: justify;\">Inorder Tree Traversal without recursion and without stack ?<\/h2>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>To traverse the tree using Morris Traversal is based on\u00a0<a href=\"https:\/\/www.wikitechy.com\/technology\/threaded-binary-tree\/\" target=\"_blank\" rel=\"noopener\">Threaded Binary Tree<\/a>\u00a0which means without using stack and recursion. In this traversal, we first create links to\u00a0<a href=\"https:\/\/www.wikitechy.com\/technology\/python-program-inorder-successor-binary-search-tree\/\" target=\"_blank\" rel=\"noopener\">Inorder successor<\/a>\u00a0and print the data using these links, and finally revert the changes to restore original tree.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>If current does not have left child\n<ul>\n<li>Print current\u2019s data<\/li>\n<li>Go to the right, i.e., current = current-&gt;right<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>Else\n<ul>\n<li>Make current as right child of the rightmost<\/li>\n<li>node in current&#8217;s left subtree<\/li>\n<li>Go to this left child, i.e., current = current-&gt;left<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>When the tree is modified through the traversal, it is reverted back to its original shape after the completion.<\/li>\n<li>Unlike Stack based traversal, no extra space is required for this traversal.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"read-also\" style=\"text-align: justify;\"><\/div>\n<div class=\"text-center row\" style=\"text-align: justify;\"><\/div>\n<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"sample-code-in-c\" class=\"color-red\" style=\"text-align: justify;\">Sample Code in C++<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">#include &lt;stdio.h&gt; <br\/>#include &lt;stdlib.h&gt; <br\/><br\/>\/* A binary tree tNode has data, pointer to left child <br\/>and a pointer to right child *\/<br\/>struct traversalNode { <br\/>\tint data; <br\/>\tstruct traversalNode* left; <br\/>\tstruct traversalNode* right; <br\/>}; <br\/><br\/>\/* Function to traverse binary tree without recursion and <br\/>without stack *\/<br\/>void MorrisTraversal(struct traversalNode* root) <br\/>{ <br\/>\tstruct traversalNode *current, *pre; <br\/><br\/>\tif (root == NULL) <br\/>\t\treturn; <br\/><br\/>\tcurrent = root; <br\/>\twhile (current != NULL) { <br\/><br\/>\t\tif (current-&gt;left == NULL) { <br\/>\t\t\tprintf(&quot;%d &quot;, current-&gt;data); <br\/>\t\t\tcurrent = current-&gt;right; <br\/>\t\t} <br\/>\t\telse { <br\/><br\/>\t\t\t\/* Find the inorder predecessor of current *\/<br\/>\t\t\tpre = current-&gt;left; <br\/>\t\t\twhile (pre-&gt;right != NULL &amp;&amp; pre-&gt;right != current) <br\/>\t\t\t\tpre = pre-&gt;right; <br\/><br\/>\t\t\t\/* Make current as right child of its inorder <br\/>\t\t\tpredecessor *\/<br\/>\t\t\tif (pre-&gt;right == NULL) { <br\/>\t\t\t\tpre-&gt;right = current; <br\/>\t\t\t\tcurrent = current-&gt;left; <br\/>\t\t\t} <br\/><br\/>\t\t\t\/* Revert the changes made in if part to restore <br\/>\t\t\tthe original tree i.e., fix the right child <br\/>\t\t\tof predecssor *\/<br\/>\t\t\telse { <br\/>\t\t\t\tpre-&gt;right = NULL; <br\/>\t\t\t\tprintf(&quot;%d &quot;, current-&gt;data); <br\/>\t\t\t\tcurrent = current-&gt;right; <br\/>\t\t\t} \/* End of if condition pre-&gt;right == NULL *\/<br\/>\t\t} \/* End of if condition current-&gt;left == NULL*\/<br\/>\t} \/* End of while *\/<br\/>} <br\/><br\/>\/* UTILITY FUNCTIONS *\/<br\/>\/* Helper function that allocates a new tNode with the <br\/>given data and NULL left and right pointers. *\/<br\/>struct traversalNode* newtraversalNode(int data) <br\/>{ <br\/>\tstruct traversalNode* node = new traversalNode; <br\/>\tnode-&gt;data = data; <br\/>\tnode-&gt;left = NULL; <br\/>\tnode-&gt;right = NULL; <br\/><br\/>\treturn (node); <br\/>} <br\/><br\/>\/* Driver program to test above functions*\/<br\/>int main() <br\/>{ <br\/><br\/>\t\/* Constructed binary tree is <br\/>\t\t\t12 <br\/>\t\t\/ \\ <br\/>\t\t13\t 14 <br\/>\t\/ \\ <br\/>\t15\t 16 <br\/>*\/<br\/>\tstruct traversalNode* root = newtraversalNode(12); <br\/>\troot-&gt;left = newtraversalNode(13); <br\/>\troot-&gt;right = newtraversalNode(14); <br\/>\troot-&gt;left-&gt;left = newtraversalNode(15); <br\/>\troot-&gt;left-&gt;right = newtraversalNode(16); <br\/><br\/>\tMorrisTraversal(root); <br\/><br\/>\treturn 0; <br\/>} <\/code><\/pre> <\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"output\" class=\"color-red\">Output<\/h2>\n<\/div>\n<\/div>\n<div class=\"Output\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<figure class=\"highlight\">\n<pre><code class=\"hljs\" data-lang=\"\"><span class=\"nt\">15 13 16 12 14 \r\n<\/span><\/code><\/pre>\n<\/figure>\n<\/div>\n<\/div>\n<div class=\"Content\">\n<div class=\"hddn\">\n<ul>\n<li style=\"text-align: justify;\">Time Complexity : O(n) If we investigate, we can see that each edge of the tree is crossed at-most multiple times.<\/li>\n<li style=\"text-align: justify;\">Also, in most pessimistic scenario same number of additional edges (as info tree) are made and evacuated.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Answer : To traverse the tree using Morris Traversal&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[3028],"tags":[195,971,491,221,3594,3598,3588,368,3596,3577,203,199,214,198,363,3057,3591,205,222,484,3054,196,212,3595,3592,3575,3586,3589,3593,3582,3590,3583,3581,3573,3580,3585,3579,3587,3578,207,366,204,3584,3574,3576,206,972,200,3055,197,3597,968,3056,285,969],"class_list":["post-507","post","type-post","status-publish","format-standard","hentry","category-data-structure","tag-accenture-interview-questions-and-answers","tag-altimetrik-india-pvt-ltd-interview-questions-and-answers","tag-applied-materials-interview-questions-and-answers","tag-bharti-airtel-interview-questions-and-answers","tag-binary-search-tree-inorder-traversal","tag-binary-search-tree-traversal-inorder-preorder-postorder-example","tag-binary-tree-inorder-traversal","tag-bmc-software-interview-questions-and-answers","tag-bst-inorder-traversal","tag-c-tree-traversal-without-recursion","tag-capgemini-interview-questions-and-answers","tag-casting-networks-india-pvt-limited-interview-questions-and-answers","tag-cgi-group-inc-interview-questions-and-answers","tag-chetu-interview-questions-and-answers","tag-ciena-corporation-interview-questions-and-answers","tag-collabera-te-interview-questions-and-answers","tag-construct-binary-tree-from-preorder-and-inorder-traversal","tag-dell-international-services-india-pvt-ltd-interview-questions-and-answers","tag-flipkart-interview-questions-and-answers","tag-genpact-interview-questions-and-answers","tag-globallogic-india-pvt-ltd-interview-questions-and-answers","tag-ibm-interview-questions-and-answers","tag-indecomm-global-services-interview-questions-and-answers","tag-inorder-preorder-postorder-traversal","tag-inorder-preorder-postorder-traversal-examples-pdf","tag-inorder-preorder-postorder-traversal-without-recursion-in-c","tag-inorder-traversal","tag-inorder-traversal-algorithm","tag-inorder-traversal-example","tag-inorder-traversal-iterative-python","tag-inorder-traversal-java","tag-inorder-traversal-python","tag-inorder-traversal-with-recursion","tag-inorder-traversal-without-recursion","tag-inorder-traversal-without-recursion-and-stack","tag-inorder-traversal-without-recursion-in-c","tag-inorder-traversal-without-recursion-using-stack-in-c","tag-inorder-tree-traversal","tag-inorder-tree-traversal-without-recursion","tag-mphasis-interview-questions-and-answers","tag-netapp-interview-questions-and-answers","tag-oracle-corporation-interview-questions-and-answers","tag-postorder-traversal-algorithm-using-stack","tag-postorder-traversal-without-recursion","tag-preorder-traversal-without-recursion","tag-sap-labs-india-pvt-ltd-interview-questions-and-answers","tag-sapient-consulting-pvt-ltd-interview-questions-and-answers","tag-tech-mahindra-interview-questions-and-answers","tag-tracxn-technologies-pvt-ltd-interview-questions-and-answers","tag-unitedhealth-group-interview-questions-and-answers","tag-what-is-inorder-traversal","tag-wipro-infotech-interview-questions-and-answers","tag-wm-global-technology-services-india-pvt-ltd-limited-wmgts-interview-questions-and-answers","tag-xoriant-solutions-pvt-ltd-interview-questions-and-answers","tag-yodlee-infotech-pvt-ltd-interview-questions-and-answers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Inorder Tree Traversal without recursion and without stack ?<\/title>\n<meta name=\"description\" content=\"Inorder Tree Traversal without recursion and without stack ? - To traverse the tree using Morris Traversal is based on Threaded Binary Tree which means without using stack and recursion.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/inorder-tree-traversal-without-recursion-and-without-stack\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Inorder Tree Traversal without recursion and without stack ?\" \/>\n<meta property=\"og:description\" content=\"Inorder Tree Traversal without recursion and without stack ? - To traverse the tree using Morris Traversal is based on Threaded Binary Tree which means without using stack and recursion.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/inorder-tree-traversal-without-recursion-and-without-stack\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-13T17:29:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-13T06:25:01+00:00\" \/>\n<meta name=\"author\" content=\"Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/inorder-tree-traversal-without-recursion-and-without-stack\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/inorder-tree-traversal-without-recursion-and-without-stack\/\",\"name\":\"Inorder Tree Traversal without recursion and without stack ?\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"datePublished\":\"2021-07-13T17:29:55+00:00\",\"dateModified\":\"2021-09-13T06:25:01+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\"},\"description\":\"Inorder Tree Traversal without recursion and without stack ? - To traverse the tree using Morris Traversal is based on Threaded Binary Tree which means without using stack and recursion.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/inorder-tree-traversal-without-recursion-and-without-stack\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\",\"name\":\"Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"caption\":\"Editor\"},\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Inorder Tree Traversal without recursion and without stack ?","description":"Inorder Tree Traversal without recursion and without stack ? - To traverse the tree using Morris Traversal is based on Threaded Binary Tree which means without using stack and recursion.","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:\/\/www.wikitechy.com\/interview-questions\/data-structure\/inorder-tree-traversal-without-recursion-and-without-stack\/","og_locale":"en_US","og_type":"article","og_title":"Inorder Tree Traversal without recursion and without stack ?","og_description":"Inorder Tree Traversal without recursion and without stack ? - To traverse the tree using Morris Traversal is based on Threaded Binary Tree which means without using stack and recursion.","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/inorder-tree-traversal-without-recursion-and-without-stack\/","og_site_name":"Wikitechy","article_published_time":"2021-07-13T17:29:55+00:00","article_modified_time":"2021-09-13T06:25:01+00:00","author":"Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Editor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/inorder-tree-traversal-without-recursion-and-without-stack\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/inorder-tree-traversal-without-recursion-and-without-stack\/","name":"Inorder Tree Traversal without recursion and without stack ?","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"datePublished":"2021-07-13T17:29:55+00:00","dateModified":"2021-09-13T06:25:01+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"description":"Inorder Tree Traversal without recursion and without stack ? - To traverse the tree using Morris Traversal is based on Threaded Binary Tree which means without using stack and recursion.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/inorder-tree-traversal-without-recursion-and-without-stack\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757","name":"Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","caption":"Editor"},"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=507"}],"version-history":[{"count":3,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/507\/revisions"}],"predecessor-version":[{"id":3493,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/507\/revisions\/3493"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}