{"id":9985,"date":"2022-09-27T10:07:44","date_gmt":"2022-09-27T10:07:44","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=9985"},"modified":"2022-12-14T09:17:01","modified_gmt":"2022-12-14T09:17:01","slug":"iterative-postorder-traversal-set-2-using-one-stack","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/","title":{"rendered":"Iterative Postorder Traversal | Set 2 (Using One Stack)"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179923079-Topic.jpg\" alt=\"\" \/><\/p>\n<h3>Problem Statement:<\/h3>\n<p>You are given a binary tree. Your task is to print the postorder traversal of the binary tree using just 1 stack.<\/p>\n<h3>Example<\/h3>\n<p>Consider the tree given below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664178715052-1-01.png\" alt=\"\" \/><br \/>\nThe postorder traversal means that we have to travel the children of every node before visiting the node in the order \u201cleft right node\u201d. Hence, the postorder traversal of the given tree is shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664178781178-1-02.png\" alt=\"\" \/><br \/>\nSo, let us now discuss the approach.<\/p>\n<h3>What is Postorder Traversal?<\/h3>\n<p>Before we directly jump into the solution, let us first discuss in detail, what postorder traversal is. Consider the tree given below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664178957749-1-03.png\" alt=\"\" \/><br \/>\nWe are currently at the root node of the tree. There are three different types of traversals. The preorder traversal says that we have to follow the order \u201croot left right\u201d. This means that as soon as we are at a node, we have to mark it visited. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664178977526-1-04.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179194997-1-05.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179225701-1-06.png\" alt=\"\" \/><br \/>\nThe images above show that as soon as we reach a node in our traversal, consider it visited. So, the complete preorder traversal is shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179241773-1-07.png\" alt=\"\" \/><br \/>\nThe inorder traversal has the order of \u201cleft root right\u201d. This means that first we explore the left subtree, then mark the current node as visited, and then visit the right subtree. This is shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179255247-1-08.png\" alt=\"\" \/><br \/>\nTraversing till here, we have not marked any node visited as we kept on visiting the left node. Now, node 4 does not have any left node. So, we can say that we have traversed its left node. Now, the order is \u201cleft node right\u201d, so after visiting the left node, we have to mark the node visited. Hence, we mark node 4 as visited.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179282395-1-09.png\" alt=\"\" \/><br \/>\nSimilarly, the entire tree is traversed.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179299505-1-10.png\" alt=\"\" \/><br \/>\nNow, the postorder traversal has the order \u201cleft right root\u201d. This means that we have to first traverse the left subtree completely, then traverse the right subtree completely, and at the last, we have to mark the current node visited.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179315828-1-11.png\" alt=\"\" \/><br \/>\nTill here, we have not visited any node because we kept on moving to the left node of every node. Now, at Node 4, we see that there is no left node. So, we can say that we have visited the left node. Also, there is no right node. Hence, we can say that we have visited the right node also. So, now we can mark the current node visited. So, Node 4 is the first node in the postorder traversal.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179338099-1-12.png\" alt=\"\" \/><br \/>\nWe keep on moving further as shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179359734-1-13.png\" alt=\"\" \/><br \/>\nAt Node 8, we don\u2019t have any left or right nodes. Hence, we can say that we have visited the left and right nodes of Node 8. So, mark it visited as well.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179372271-1-14.png\" alt=\"\" \/><br \/>\nNow, we reach node 5 again. Here, we can see that we have visited the left subtree of 5. There is no right child for Node 5. Hence, we can say that we have visited both the children. So, let us include 5 in the postorder traversal too.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179384195-1-15.png\" alt=\"\" \/><br \/>\nWhen we are at Node 2, we have already visited the left subtree (Node 4) and the right subtree (starting from Node 5) as well. So, we can now mark Node 2 as visited.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179400926-1-16%20%281%29.png\" alt=\"\" \/><br \/>\nSimilarly, you can complete the rest of the traversal as well. The complete postorder traversal of the tree is shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179415973-1-16%20%282%29.png\" alt=\"\" \/><br \/>\nSo, for a better understanding, have a look at the image shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179464049-1-16%20%283%29.png\" alt=\"\" \/><br \/>\nWhenever we are on the left of any node i.e. no child has been traversed yet, we say that we are in a pre-node region. Whenever we are in the middle of the left and right child of a node i.e. the left subtree has been traversed but the right subtree has not been traversed, we are in the in-node region. Similarly, whenever we are in the right of a node i.e. both its children have been traversed, we are in the post-node region.<\/p>\n<p>Now that we have a complete understanding of different tree traversals, let us approach our problem i.e. postorder traversal using 2 stacks.<\/p>\n<h3>Approach (Using 1 Stack)<\/h3>\n<p>We know that the postorder traversal is \u201cleft-right root\u201d. This means that we have to try to go to the left of the given tree till it is possible. Then, we have to traverse the right of the tree till it is possible. After traversing both left and right, we have to print. So, the algorithm that we are going to use depends highly on our attempts to maintain the \u201cleft-right root\u201d order.<\/p>\n<p>Let us see what the algorithm is.<\/p>\n<h3>Algorithm:<\/h3>\n<ol>\n<li>Assign a variable current to the root of the tree and initialize a stack of nodes (initially empty).<\/li>\n<li>While current does not become null OR the stack is not empty<br \/>\na. If the current is not equal to null<br \/>\ni. Push current into the stack.<br \/>\nii. Move current to its left.<br \/>\nb. Else <\/p>\n<ul>\n<li>Assign a variable temp to the right child at the top of the stack.<\/li>\n<li>So, the top of the stack might or might not have the right child. So, if the temp is null\n<ol>\n<li>temp = stack.top()<\/li>\n<li>stack.pop()<\/li>\n<li>Add temp to the postorder<\/li>\n<li>While the stack is not empty AND the temp variable is equal to the right child of the top of the stack,<br \/>\na. temp = stack.top()<br \/>\nb. stack.pop()<br \/>\nc. Add temp to the postorder<br \/>\niii. Else, current  = temp.<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>So, this algorithm helps maintain the postorder i.e. \u201cleft-right root\u201d. Let us see an example dry run for the above algorithm.<\/p>\n<h3>Dry Run<\/h3>\n<p>Consider the following tree shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179484698-1-16%20%284%29.png\" alt=\"\" \/><br \/>\nSo, we will start by having the variable curr (current) at the root of the tree.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179497625-1-16%20%285%29.png\" alt=\"\" \/><br \/>\nSo, according to the first step of the algorithm, since current is not equal to null, we add it to the stack and move it to its left.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179515858-1-16%20%286%29.png\" alt=\"\" \/><br \/>\nWe keep on repeating this step as shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179535331-1-16%20%287%29.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179556175-1-16%20%288%29.png\" alt=\"\" \/><br \/>\nSo, the value of current is now null as after pushing 30 into the stack, it moved to its left child which is null. Now, we follow the else condition in the algorithm. So, we will take a variable temp that will be the right child of the top of the stack. Since the top of the stack is 30, temp = right child of 30 i.e. 40.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179674144-1-16%20%289%29.png\" alt=\"\" \/><br \/>\nSince a temp is not equal to null, current will now move to temp.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179689157-1-16%20%2810%29.png\" alt=\"\" \/><br \/>\nSince the current is now not null, we add it to the stack and move to its left child.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179705913-1-16%20%2811%29.png\" alt=\"\" \/><\/p>\n<p>So, we see that again, the steps will be repeated. This means that every time, we try to go to the left child of every node and if the left is null, we move to its right. This will keep happening till we reach Node 60.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179803515-1-16%20%2812%29.png\" alt=\"\" \/><\/p>\n<p>Now, the temp will be the right of the top of the stack. This means the temp will be the right of 60. So, temp = null.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179819941-1-16%20%2813%29.png\" alt=\"\" \/><br \/>\nSince temp = null, we pop a value from the stack and it becomes temp and we also add it to the postorder.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179838445-1-16%20%2814%29.png\" alt=\"\" \/><br \/>\nNow, while the stack is not empty AND temp is equal to the right child of the top of the stack, we have to keep popping from the stack and adding the node to the post order.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179854263-1-16%20%2815%29.png\" alt=\"\" \/><br \/>\nSo, 50 was popped and added to the postorder. Now, we will pop 40 since the right child of 40 is equal to temp.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179868807-1-16%20%2816%29.png\" alt=\"\" \/><br \/>\nThis will continue as shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179881613-1-16%20%2817%29.png\" alt=\"\" \/><br \/>\nNow the right child of the top of the stack is null but temp = 30 (Node 30 not value). So, again, we see that curr = null and we follow the above-discussed steps.<br \/>\nYou can complete the rest of the traversal yourself and you will find that we end up having the postorder of the tree correctly.<\/p>\n<p>So, now that we have understood the procedure, let us write the code for the same.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_9973 {\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_9973 .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_9973 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_9973 .wpsm_nav-tabs > li.active > a, #tab_container_9973 .wpsm_nav-tabs > li.active > a:hover, #tab_container_9973 .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_9973 .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_9973 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_9973 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_9973 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_9973 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_9973 .wpsm_nav-tabs > li > a:hover , #tab_container_9973 .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_9973 .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_9973 .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_9973 .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_9973 .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_9973 .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_9973 .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_9973 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_9973 .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_9973 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_9973 .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_9973 .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_9973\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_9973\">\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_9973_1\" aria-controls=\"tabs_desc_9973_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_9973_2\" aria-controls=\"tabs_desc_9973_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>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_9973\">\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_9973_1\">\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\n#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n\r\nstruct Node {\r\n\tint data;\r\n\tstruct Node *left, *right;\r\n};\r\n\r\nstruct Node* newNode(int data)\r\n{\r\n\tstruct Node* node = new Node;\r\n\tnode-&gt;data = data;\r\n\tnode-&gt;left = node-&gt;right = NULL;\r\n\treturn node;\r\n}\r\n\r\nvector&lt;int&gt; postOrderIterative(struct Node* root)\r\n{\r\n\tvector&lt;int&gt; postOrderList;\r\n\tif (root == NULL)\r\n\t\treturn postOrderList;\r\n\r\n    stack&lt;Node*&gt; stk;\r\n\tstk.push(root);\r\n\tNode* prev = NULL;\r\n\twhile (!stk.empty()) {\r\n\t\tauto current = stk.top();\r\n\t\t\r\n        if (prev == NULL || prev-&gt;left == current\r\n\t\t\t|| prev-&gt;right == current) {\r\n\t\t\tif (current-&gt;left)\r\n\t\t\t\tstk.push(current-&gt;left);\r\n\t\t\telse if (current-&gt;right)\r\n\t\t\t\tstk.push(current-&gt;right);\r\n\t\t\telse {\r\n\t\t\t\tstk.pop();\r\n\t\t\t\tpostOrderList.push_back(current-&gt;data);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\telse if (current-&gt;left == prev) {\r\n\t\t\tif (current-&gt;right)\r\n\t\t\t\tstk.push(current-&gt;right);\r\n\t\t\telse {\r\n\t\t\t\tstk.pop();\r\n\t\t\t\tpostOrderList.push_back(current-&gt;data);\r\n\t\t\t}\r\n\r\n\t\t\t\t\t}\r\n\t\telse if (current-&gt;right == prev) {\r\n\t\t\tstk.pop();\r\n\t\t\tpostOrderList.push_back(current-&gt;data);\r\n\t\t}\r\n\t\tprev = current;\r\n\t}\r\n\treturn postOrderList;\r\n}\r\n\r\nint main()\r\n{\r\n\t\r\n\tstruct Node* root = NULL;\r\n    root = newNode(10);\r\n\t  root-&gt;left = newNode(20);\r\n    root-&gt;left-&gt;left = newNode(30);\r\n    root-&gt;left-&gt;left-&gt;right = newNode(40);\r\n    root-&gt;left-&gt;left-&gt;right-&gt;right = newNode(50);\r\n    root-&gt;left-&gt;left-&gt;right-&gt;right-&gt;right = newNode(60);\r\n    root-&gt;right = newNode(70);\r\n    root-&gt;right-&gt;left = newNode(80);\r\n    \r\n\tprintf(&quot;Post order traversal of binary tree is :&#92;n&quot;);\r\n\tprintf(&quot;[&quot;);\r\n\tvector&lt;int&gt; postOrderList = postOrderIterative(root);\r\n\tfor (auto it : postOrderList)\r\n\t\tcout &lt;&lt; it &lt;&lt; &quot; &quot;;\r\n\tprintf(&quot;]&quot;);\r\n\treturn 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_9973_2\">\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\nimport java.util.ArrayList;\r\nimport java.util.Stack;\r\n\r\n\r\nclass Node {\r\n\tint data;\r\n\tNode left, right;\r\n\r\n\tNode(int item)\r\n\t{\r\n\t\tdata = item;\r\n\t\tleft = right;\r\n\t}\r\n}\r\n\r\nclass BinaryTree {\r\n\tNode root;\r\n\tArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();\r\n\r\n\tArrayList&lt;Integer&gt; postOrderIterative(Node node)\r\n\t{\r\n\t\tStack&lt;Node&gt; stk = new Stack&lt;Node&gt;();\r\n\r\n\t\t\/\/ Check for empty tree\r\n\t\tif (node == null)\r\n\t\t\treturn list;\r\n        \r\n\t\tstk.push(node);\r\n\t\tNode prev = null;\r\n\t\twhile (!stk.isEmpty()) {\r\n\t\t\tNode current = stk.peek();\r\n\r\n\t\t\tif (prev == null || prev.left == current || prev.right == current) {\r\n\t\t\t\tif (current.left != null)\r\n\t\t\t\t\tstk.push(current.left);\r\n\t\t\t\telse if (current.right != null)\r\n\t\t\t\t\tstk.push(current.right);\r\n\t\t\t\telse {\r\n\t\t\t\t\tstk.pop();\r\n\t\t\t\t\tlist.add(current.data);\r\n\t\t\t\t}\r\n\r\n\t\t\t}\r\n\t\t\telse if (current.left == prev) {\r\n\t\t\t\tif (current.right != null)\r\n\t\t\t\t\tstk.push(current.right);\r\n\t\t\t\telse {\r\n\t\t\t\t\tstk.pop();\r\n\t\t\t\t\tlist.add(current.data);\r\n\t\t\t\t}\r\n\r\n\t\t\t}\r\n\t\t\telse if (current.right == prev) {\r\n\t\t\t\tstk.pop();\r\n\t\t\t\tlist.add(current.data);\r\n\t\t\t}\r\n\r\n\t\t\tprev = current;\r\n\t\t}\r\n\r\n\t\treturn list;\r\n\t}\r\n\r\n\tpublic static void main(String args[])\r\n\t{\r\n\t\tBinaryTree tree = new BinaryTree();\r\n\r\n\t\t\/\/ Let us create trees shown in above diagram\r\n\t\ttree.root = new Node(10);\r\n\t\ttree.root.left = new Node(20);\r\n        tree.root.left.left = new Node(30);\r\n        tree.root.left.left.right = new Node(40);\r\n        tree.root.left.left.right.right = new Node(50);\r\n        tree.root.left.left.right.right.right = new Node(60);\r\n        tree.root.right = new Node(70);\r\n        tree.root.right.left = new Node(80);\r\n\r\n\t\tArrayList&lt;Integer&gt; mylist = tree.postOrderIterative(tree.root);\r\n\r\n\t\tSystem.out.println(\r\n\t\t\t&quot;Post order traversal of binary tree is :&quot;);\r\n\t\tSystem.out.println(mylist);\r\n\t}\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_9973 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_9973 a\"),jQuery(\"#tab-content_9973\"));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<p><strong>Time Complexity:<\/strong> The time complexity of this procedure is O(N) as we have to push and pop elements from the stack and the maximum height of the stack can be O(N).<\/p>\n<p><strong>Space Complexity:<\/strong> The space complexity of this procedure is O(N) as we are using a Stack to perform our operation.<\/p>\n<p>We tried to discuss Iterative Postorder Traversal. We hope this article gives you a better understanding of Iterative Postorder Traversal, <a href=\"http:\/https:\/\/www.prepbytes.com\/\/\" title=\"PrepBytes\">PrepBytes<\/a> also provides a good collection of <a href=\"http:\/\/https:\/\/www.prepbytes.com\/prepbytes-courses\" title=\"Foundation Courses\">Foundation Courses<\/a> that can help you enhance your coding skills. Want to make sure you ace the interview in one go? Join our <a href=\"http:\/\/https:\/\/www.prepbytes.com\/placement-preparation-program\" title=\"Placement Program\">Placement Program<\/a> that will help you get prepared and land your dream job at MNC\u2019s. Mentors of Prepbytes are highly experienced and can provide you with basic, in-depth subject knowledge for better understanding.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement: You are given a binary tree. Your task is to print the postorder traversal of the binary tree using just 1 stack. Example Consider the tree given below. The postorder traversal means that we have to travel the children of every node before visiting the node in the order \u201cleft right node\u201d. Hence, [&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":[1],"tags":[],"class_list":["post-9985","post","type-post","status-publish","format-standard","hentry","category-miscellaneous"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Iterative Postorder Traversal | Set 2 (Using One Stack) | Miscellaneous | PrepBytes Blog<\/title>\n<meta name=\"description\" content=\"In this article, we will be discussing Iterative Postorder Traversal, Also we have discussed a simple Iterative Postorder Traversal using two stacks in the previous post.\" \/>\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\/iterative-postorder-traversal-set-2-using-one-stack\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Iterative Postorder Traversal | Set 2 (Using One Stack) | Miscellaneous | PrepBytes Blog\" \/>\n<meta property=\"og:description\" content=\"In this article, we will be discussing Iterative Postorder Traversal, Also we have discussed a simple Iterative Postorder Traversal using two stacks in the previous post.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/\" \/>\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=\"2022-09-27T10:07:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-14T09:17:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179923079-Topic.jpg\" \/>\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=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Iterative Postorder Traversal | Set 2 (Using One Stack)\",\"datePublished\":\"2022-09-27T10:07:44+00:00\",\"dateModified\":\"2022-12-14T09:17:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/\"},\"wordCount\":1426,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179923079-Topic.jpg\",\"articleSection\":[\"Miscellaneous\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/\",\"name\":\"Iterative Postorder Traversal | Set 2 (Using One Stack) | Miscellaneous | PrepBytes Blog\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179923079-Topic.jpg\",\"datePublished\":\"2022-09-27T10:07:44+00:00\",\"dateModified\":\"2022-12-14T09:17:01+00:00\",\"description\":\"In this article, we will be discussing Iterative Postorder Traversal, Also we have discussed a simple Iterative Postorder Traversal using two stacks in the previous post.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179923079-Topic.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179923079-Topic.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Miscellaneous\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/miscellaneous\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Iterative Postorder Traversal | Set 2 (Using One Stack)\"}]},{\"@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":"Iterative Postorder Traversal | Set 2 (Using One Stack) | Miscellaneous | PrepBytes Blog","description":"In this article, we will be discussing Iterative Postorder Traversal, Also we have discussed a simple Iterative Postorder Traversal using two stacks in the previous post.","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\/iterative-postorder-traversal-set-2-using-one-stack\/","og_locale":"en_US","og_type":"article","og_title":"Iterative Postorder Traversal | Set 2 (Using One Stack) | Miscellaneous | PrepBytes Blog","og_description":"In this article, we will be discussing Iterative Postorder Traversal, Also we have discussed a simple Iterative Postorder Traversal using two stacks in the previous post.","og_url":"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2022-09-27T10:07:44+00:00","article_modified_time":"2022-12-14T09:17:01+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179923079-Topic.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Iterative Postorder Traversal | Set 2 (Using One Stack)","datePublished":"2022-09-27T10:07:44+00:00","dateModified":"2022-12-14T09:17:01+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/"},"wordCount":1426,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179923079-Topic.jpg","articleSection":["Miscellaneous"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/","url":"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/","name":"Iterative Postorder Traversal | Set 2 (Using One Stack) | Miscellaneous | PrepBytes Blog","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179923079-Topic.jpg","datePublished":"2022-09-27T10:07:44+00:00","dateModified":"2022-12-14T09:17:01+00:00","description":"In this article, we will be discussing Iterative Postorder Traversal, Also we have discussed a simple Iterative Postorder Traversal using two stacks in the previous post.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179923079-Topic.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1664179923079-Topic.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/iterative-postorder-traversal-set-2-using-one-stack\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Miscellaneous","item":"https:\/\/prepbytes.com\/blog\/category\/miscellaneous\/"},{"@type":"ListItem","position":3,"name":"Iterative Postorder Traversal | Set 2 (Using One Stack)"}]},{"@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\/9985","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=9985"}],"version-history":[{"count":4,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/9985\/revisions"}],"predecessor-version":[{"id":10043,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/9985\/revisions\/10043"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=9985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=9985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=9985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}