{"id":13281,"date":"2023-02-17T12:11:04","date_gmt":"2023-02-17T12:11:04","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=13281"},"modified":"2023-11-01T12:02:51","modified_gmt":"2023-11-01T12:02:51","slug":"tree-traversal-inorder-preorder-postorder","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/","title":{"rendered":"Tree Traversal : Inorder, Preorder and Postorder"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612025952-Tree%20Traversal.jpg\" alt=\"\" \/><\/p>\n<p>Tree traversal refers to the procedure of navigating through all the nodes within a tree data structure, ensuring that every node is visited along the way. Various methods of traversing a tree include Inorder Traversal, Preorder Traversal, and Postorder Traversal.<\/p>\n<h2>What is a Tree?<\/h2>\n<p>A tree is a hierarchical data structure characterized by its non-directional nature, with nodes organized in a hierarchical, level-based order. The uppermost node in the tree is referred to as the root node, and nodes that are directly connected above and below it are known as the parent and child nodes, respectively.<\/p>\n<p>The two nodes with the same parent node are known as siblings. Another important terminology to know is a leaf node, a node that has no children. Below is an illustration that can help you understand the tree data structure well to proceed with traversals that can be performed.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026087-Tree%20Traversal1.png\" alt=\"\" \/><\/p>\n<h2>Inorder Traversal<\/h2>\n<p>In this form of traversal, the nodes are traversed such that the sequence is in increasing or non-decreasing order. <strong>Left-&gt;Root-&gt;Right<\/strong> pattern is followed to traverse the nodes as we can go as further to left until there is no left or smaller value to pick from.<\/p>\n<p>On returning back to the parent node, the nodes of the right are traversed in a similar manner and the pattern follows for each node in the tree until we are done traversing the tree.<\/p>\n<h3>Dry Run of Inorder Traversal<\/h3>\n<p>Taking an example of the given tree below, we go over step by step to see how the inorder traversal is performed on a tree.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026087-Tree%20Traversal2.png\" alt=\"\" \/><\/p>\n<p>We keep moving to the left till there is no left element.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026097-Tree%20Traversal3.png\" alt=\"\" \/><\/p>\n<p>As we see in the above figure, 0 is the smallest and there is no element smaller than that on left, we output and go back.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026097-Tree%20Traversal4.png\" alt=\"\" \/><\/p>\n<p>Moving to the right subtree, we go on from 2 to 5 printing 2 is the process.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026121-Tree%20Traversal5.png\" alt=\"\" \/><\/p>\n<p>Being on the leaf node, we can no longer go further thus, print 5 and return back to the parent node.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026121-Tree%20Traversal6.png\" alt=\"\" \/><\/p>\n<p>Having completed the left subtree of the node we return to the node successfully to travel further.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026127-Tree%20Traversal7.png\" alt=\"\" \/><\/p>\n<p>Printing 9 or the root node and diving to the right subtree, we head to node value 12 on the right.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026127-Tree%20Traversal8.png\" alt=\"\" \/><\/p>\n<p>Going as further to left in the subtree, we step towards the next node on the left.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026127-Tree%20Traversal9.png\" alt=\"\" \/><\/p>\n<p>As we are currently on leaf node, we return back as we are out of path.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026127-Tree%20Traversal10.png\" alt=\"\" \/><\/p>\n<p>Completing the left subtree of node with value 12, we print the value and traverse to the right.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026136-Tree%20Traversal11.png\" alt=\"\" \/><\/p>\n<p>We print and return back to the initial or node to finish the process of Inorder Traversal<\/p>\n<h3>Algorithm of Inorder Traversal<\/h3>\n<ol>\n<li>Starting at the left subtree, the Inorder function is called on it.<\/li>\n<li>The root node is then visited.<\/li>\n<li>Lastly, the Inorder function is called on the right subtree.<\/li>\n<\/ol>\n<h2>Preorder Traversal<\/h2>\n<p>In the preorder traversal of a binary tree, the nodes are visited in the following sequence: <strong>Root-&gt;Left-&gt;Right.<\/strong> The algorithm starts by visiting or printing the root node, and then traversing the left subtree. <\/p>\n<p>Once the left subtree has been fully traversed, the algorithm then moves on to traverse the right subtree. This pattern is repeated for each node in the tree until all nodes have been visited. In this way, the preorder traversal visits the root node first, followed by the left subtree, and finally the right subtree. This pattern allows for a quick traversal of the entire tree, starting from the root and moving down towards the leaves.<\/p>\n<h3>Algorithm for Preorder Traversal<\/h3>\n<ol>\n<li>Visit the root node. <\/li>\n<li>Traverse the left subtree by recursively calling the preorder function on the left child. <\/li>\n<li>Traverse the right subtree by recursively calling the preorder function on the right child.<\/li>\n<\/ol>\n<h3>Dry Run of Preoder Traveral<\/h3>\n<p>Looking in a step by step manner onhow we can solve the preorer traversal.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026136-Tree%20Traversal12.png\" alt=\"\" \/><\/p>\n<p>Traversing from the root node, we move to the left of 2.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026136-Tree%20Traversal13.png\" alt=\"\" \/><\/p>\n<p>Going from the node 0, we found nothing, so returning back to the parents.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026137-Tree%20Traversal14.png\" alt=\"\" \/><\/p>\n<p>On returing to noe with value 2 from left subree, we move to the right subtree.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026137-Tree%20Traversal15.png\" alt=\"\" \/><\/p>\n<p>As, 5 is visited, we add it to visited or print it and move back as it is a leaf node.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026155-Tree%20Traversal16.png\" alt=\"\" \/><\/p>\n<p>Now moving back to the root node as the left subtree from root is already traversed successfully.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026155-Tree%20Traversal17.png\" alt=\"\" \/><\/p>\n<p>Now moving forward the right part of the root node.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026159-Tree%20Traversal18.png\" alt=\"\" \/><\/p>\n<p>We go deep down to the left node attached to the node with value 12.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026159-Tree%20Traversal19.png\" alt=\"\" \/><\/p>\n<p>As the current node, being a leaf node, we return back to the parent.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026165-Tree%20Traversal20.png\" alt=\"\" \/><\/p>\n<p>On moving to the right subtree of node with value 12, we encounter 16.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026165-Tree%20Traversal21.png\" alt=\"\" \/><\/p>\n<p>Now that we are done with the preorder traversal, we return back to the node to wind up our traversal.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026165-Tree%20Traversal22.png\" alt=\"\" \/><\/p>\n<h2>Postorder Traversal<\/h2>\n<p>In the postorder traversal of a binary tree, the nodes are visited in the following sequence: <strong>Left-&gt;Right-&gt;Root.<\/strong><\/p>\n<p>The algorithm starts by visiting or traversing the left subtree, then moving on to traverse the right subtree. Once the right subtree has been fully traversed, the algorithm then visits the root node. This pattern is repeated for each node in the tree until all nodes have been visited. <\/p>\n<p>In this way, the postorder traversal visits the left subtree first, followed by the right subtree, and finally the root node. This pattern allows for a quick traversal of the entire tree, starting from the leaves and moving up towards the root.<\/p>\n<h3>Algorithm for Postorder Traversal<\/h3>\n<ol>\n<li>Traverse the left subtree by recursively calling the postorder function on the left child.<\/li>\n<li>Traverse the right subtree by recursively calling the postorder function on the right child.<\/li>\n<li>Visit the root node.<\/li>\n<\/ol>\n<h3>Dry Run of Postorder Traversal<\/h3>\n<p>Starting the traversal from the root node towards the left side of the tree.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026171-Tree%20Traversal23.png\" alt=\"\" \/><\/p>\n<p>We hop onto the node having value 0 from the node having value 0.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026177-Tree%20Traversal24.png\" alt=\"\" \/><\/p>\n<p>As 0 is a leaf node, By the concept of Left-Right-Root, we add 0 to visited or print in our case to the output.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026177-Tree%20Traversal25.png\" alt=\"\" \/><\/p>\n<p>Jumping towards the right subtree of 2, we traverse to 5.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026177-Tree%20Traversal26.png\" alt=\"\" \/><\/p>\n<p>Being another leaf node, we print 5 in the process.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026190-Tree%20Traversal27.png\" alt=\"\" \/><\/p>\n<p>On completing the left subtree of the root node, we return back to the position where we started.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026191-Tree%20Traversal28.png\" alt=\"\" \/><\/p>\n<p>Covering the right half of the subtree, we start to traverse towards 12.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026196-Tree%20Traversal29.png\" alt=\"\" \/><\/p>\n<p>Traversing to left from node with value 12 to node with value 11.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026196-Tree%20Traversal30.png\" alt=\"\" \/><\/p>\n<p>Being a leaf node, we print 11 and return back to its parents.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026200-Tree%20Traversal31.png\" alt=\"\" \/><\/p>\n<p>Now hopping onto the right subtree of the node with value 12, we move towards 16.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026200-Tree%20Traversal32.png\" alt=\"\" \/><\/p>\n<p>Being a leaf node, we will print the value and keep returning back with keeping in mind to print the value.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026212-Tree%20Traversal33.png\" alt=\"\" \/><\/p>\n<p>12 is printed as we are done with traversing the left and right subtrees of the node.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026213-Tree%20Traversal34.png\" alt=\"\" \/><\/p>\n<p>The root node is visited at the very end according to the traversal technique of Postorder Traversal. In this process, we are done with Postorder Traversal.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612026213-Tree%20Traversal35.png\" alt=\"\" \/><\/p>\n<h2>Code for Tree Traversals<\/h2>\n<p>As we have seen the algorithm and dry run for each of the three techniques, now let us look at the code to implement all three traversals, Inorder Traversal, Postorder Traversal and Preorder Traversal in an iterative and recursive manner. <\/p>\n<p>Here is an implementation of the iterative code for inorder, postorder, and preorder traversals in Python, using the binary tree with values 9, 2, 12, 0, 5, 11, and 16.<\/p>\n<p><strong>Link to Iterative Code:<\/strong><\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_13227 {\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_13227 .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_13227 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_13227 .wpsm_nav-tabs > li.active > a, #tab_container_13227 .wpsm_nav-tabs > li.active > a:hover, #tab_container_13227 .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_13227 .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_13227 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_13227 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_13227 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_13227 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_13227 .wpsm_nav-tabs > li > a:hover , #tab_container_13227 .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_13227 .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_13227 .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_13227 .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_13227 .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_13227 .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_13227 .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_13227 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_13227 .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_13227 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_13227 .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_13227 .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_13227\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_13227\">\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_13227_1\" aria-controls=\"tabs_desc_13227_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-laptop\"><\/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_13227\">\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_13227_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># Definition for a binary tree node.\r\nclass TreeNode:\r\n    def __init__(self, x):\r\n        self.val = x\r\n        self.left = None\r\n        self.right = None\r\n\r\n# Create the binary tree\r\nroot = TreeNode(9)\r\nroot.left = TreeNode(2)\r\nroot.right = TreeNode(12)\r\nroot.left.left = TreeNode(0)\r\nroot.left.right = TreeNode(5)\r\nroot.right.left = TreeNode(11)\r\nroot.right.right = TreeNode(16)\r\n\r\n# Inorder Traversal\r\ndef inorderTraversal(root):\r\n    result, stack = [], []\r\n    while True:\r\n        while root:\r\n            stack.append(root)\r\n            root = root.left\r\n        if not stack:\r\n            return result\r\n        node = stack.pop()\r\n        result.append(node.val)\r\n        root = node.right\r\n\r\n# Postorder Traversal\r\ndef postorderTraversal(root):\r\n    result, stack = [], [(root, False)]\r\n    while stack:\r\n        node, visited = stack.pop()\r\n        if node is None:\r\n            continue\r\n        if visited:\r\n            result.append(node.val)\r\n        else:\r\n            stack.append((node, True))\r\n            stack.append((node.right, False))\r\n            stack.append((node.left, False))\r\n    return result\r\n\r\n# Preorder Traversal\r\ndef preorderTraversal(root):\r\n    result, stack = [], [root]\r\n    while stack:\r\n        node = stack.pop()\r\n        if node is None:\r\n            continue\r\n        result.append(node.val)\r\n        stack.append(node.right)\r\n        stack.append(node.left)\r\n    return result\r\n\r\n# Print the results\r\nprint(\"Inorder Traversal:\", inorderTraversal(root))\r\nprint(\"Postorder Traversal:\", postorderTraversal(root))\r\nprint(\"Preorder Traversal:\", preorderTraversal(root))\r\n\r\n<\/pre>\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_13227 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_13227 a\"),jQuery(\"#tab-content_13227\"));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>Output:<\/strong><\/p>\n<pre><code>Inorder Traversal: [0, 2, 5, 9, 11, 12, 16]\nPostorder Traversal: [0, 5, 2, 11, 16, 12, 9]\nPreorder Traversal: [9, 2, 0, 5, 12, 11, 16]<\/code><\/pre>\n<p><strong>Link to Recursive Code:<\/strong><\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_13232 {\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_13232 .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_13232 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_13232 .wpsm_nav-tabs > li.active > a, #tab_container_13232 .wpsm_nav-tabs > li.active > a:hover, #tab_container_13232 .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_13232 .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_13232 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_13232 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_13232 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_13232 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_13232 .wpsm_nav-tabs > li > a:hover , #tab_container_13232 .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_13232 .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_13232 .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_13232 .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_13232 .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_13232 .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_13232 .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_13232 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_13232 .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_13232 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_13232 .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_13232 .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_13232\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_13232\">\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_13232_1\" aria-controls=\"tabs_desc_13232_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>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_13232\">\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_13232_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># your code goes here\r\n# your code goes here# Definition for a binary tree node.\r\nclass TreeNode:\r\n    def __init__(self, val=0, left=None, right=None):\r\n        self.val = val\r\n        self.left = left\r\n        self.right = right\r\n\r\ndef inorderTraversal(root: TreeNode):\r\n    res = []\r\n    def helper_inorder(node):\r\n        if not node:\r\n            return\r\n        helper_inorder(node.left)\r\n        res.append(node.val)\r\n        helper_inorder(node.right)\r\n    helper_inorder(root)\r\n    return res\r\n\r\ndef postorderTraversal(root: TreeNode):\r\n    res = []\r\n    def helper_postorder(node):\r\n        if not node:\r\n            return\r\n        helper_postorder(node.left)\r\n        helper_postorder(node.right)\r\n        res.append(node.val)\r\n    helper_postorder(root)\r\n    return res\r\n\r\ndef preorderTraversal(root: TreeNode):\r\n    res = []\r\n    def helper_preorder(node):\r\n        if not node:\r\n            return\r\n        res.append(node.val)\r\n        helper_preorder(node.left)\r\n        helper_preorder(node.right)\r\n    helper_preorder(root)\r\n    return res\r\n\r\n# Example usage with input tree [9, 2, 12, 0, 5, 11, 16]\r\nroot = TreeNode(9)\r\nroot.left = TreeNode(2)\r\nroot.right = TreeNode(12)\r\nroot.left.left = TreeNode(0)\r\nroot.left.right = TreeNode(5)\r\nroot.right.left = TreeNode(11)\r\nroot.right.right = TreeNode(16)\r\n\r\nprint(\"Inorder Traversal:\",inorderTraversal(root)) \r\nprint(\"Postorder Traversal:\",postorderTraversal(root)) \r\nprint(\"Prerder Traversal:\",preorderTraversal(root)) \r\n<\/pre>\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_13232 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_13232 a\"),jQuery(\"#tab-content_13232\"));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>Output:<\/strong><\/p>\n<pre><code>Inorder Traversal: [0, 2, 5, 9, 11, 12, 16]\nPostorder Traversal: [0, 5, 2, 11, 16, 12, 9]\nPreorder Traversal: [9, 2, 0, 5, 12, 11, 16]<\/code><\/pre>\n<p><strong>Conclusion<\/strong><br \/>\nIn conclusion, tree traversal, encompassing Inorder, Preorder, and Postorder traversal methods, is a fundamental concept in working with tree data structures. These traversal techniques enable the systematic exploration of all nodes within a tree, ensuring that each node is visited precisely once. Tree traversal plays a pivotal role in a wide range of computer science applications, including data retrieval, searching, and tree-based algorithms.<\/p>\n<h2>Frequently Asked Questions related to Tree Traversal : Inorder, Preorder and Postorder:<\/h2>\n<p>Here are some of the FAQs related to Tree Traversal : Inorder, Preorder and Postorder<\/p>\n<p><strong>1. Why is tree traversal important in computer science?<\/strong><br \/>\nTree traversal is vital for accessing and processing data within tree structures efficiently. It&#8217;s used in various algorithms, such as searching, sorting, and parsing, and plays a crucial role in data retrieval.<\/p>\n<p><strong>2. What is the significance of the root node in a tree structure?<\/strong><br \/>\nThe root node is the topmost node in a tree and serves as the entry point for accessing all other nodes. It defines the hierarchical structure and relationships within the tree.<\/p>\n<p><strong>3. How is tree traversal implemented?<\/strong><br \/>\nTree traversal can be implemented using either a recursive or an iterative approach. In a recursive approach, the algorithm uses nested function calls to traverse the tree, visiting each node and its children in a specific order. In an iterative approach, the algorithm uses a stack data structure to keep track of which nodes need to be visited next.<\/p>\n<p><strong>4. When is tree traversal used?<\/strong><br \/>\nTree traversal is used in a variety of algorithms and data structures that involve trees, such as searching for a specific node in the tree, finding the lowest common ancestor of two nodes, or evaluating mathematical expressions stored in a binary tree. It is also used in tree data structures for processing and manipulating hierarchical data, such as XML or JSON files.<\/p>\n<p><strong>5. Are there variations or optimizations of tree traversal algorithms?<\/strong><br \/>\nYes, there are variations and optimizations of tree traversal algorithms, including iterative and recursive implementations, as well as Morris Traversal for binary trees, which reduces space complexity. The choice of method depends on specific use cases and requirements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tree traversal refers to the procedure of navigating through all the nodes within a tree data structure, ensuring that every node is visited along the way. Various methods of traversing a tree include Inorder Traversal, Preorder Traversal, and Postorder Traversal. What is a Tree? A tree is a hierarchical data structure characterized by its non-directional [&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":[153],"tags":[],"class_list":["post-13281","post","type-post","status-publish","format-standard","hentry","category-tree"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Tree Traversal : Inorder, Preorder and Postorder<\/title>\n<meta name=\"description\" content=\"Tree Traversal is the process of travelling across all the nodes in a tree data structure such that no node is left unvisited in the path.\" \/>\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\/tree-traversal-inorder-preorder-postorder\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tree Traversal : Inorder, Preorder and Postorder\" \/>\n<meta property=\"og:description\" content=\"Tree Traversal is the process of travelling across all the nodes in a tree data structure such that no node is left unvisited in the path.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/\" \/>\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=\"2023-02-17T12:11:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-01T12:02:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612025952-Tree%20Traversal.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=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Tree Traversal : Inorder, Preorder and Postorder\",\"datePublished\":\"2023-02-17T12:11:04+00:00\",\"dateModified\":\"2023-11-01T12:02:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/\"},\"wordCount\":1575,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612025952-Tree%20Traversal.jpg\",\"articleSection\":[\"Trees\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/\",\"name\":\"Tree Traversal : Inorder, Preorder and Postorder\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612025952-Tree%20Traversal.jpg\",\"datePublished\":\"2023-02-17T12:11:04+00:00\",\"dateModified\":\"2023-11-01T12:02:51+00:00\",\"description\":\"Tree Traversal is the process of travelling across all the nodes in a tree data structure such that no node is left unvisited in the path.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612025952-Tree%20Traversal.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612025952-Tree%20Traversal.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Trees\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/tree\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Tree Traversal : Inorder, Preorder and Postorder\"}]},{\"@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":"Tree Traversal : Inorder, Preorder and Postorder","description":"Tree Traversal is the process of travelling across all the nodes in a tree data structure such that no node is left unvisited in the path.","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\/tree-traversal-inorder-preorder-postorder\/","og_locale":"en_US","og_type":"article","og_title":"Tree Traversal : Inorder, Preorder and Postorder","og_description":"Tree Traversal is the process of travelling across all the nodes in a tree data structure such that no node is left unvisited in the path.","og_url":"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-02-17T12:11:04+00:00","article_modified_time":"2023-11-01T12:02:51+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612025952-Tree%20Traversal.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Tree Traversal : Inorder, Preorder and Postorder","datePublished":"2023-02-17T12:11:04+00:00","dateModified":"2023-11-01T12:02:51+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/"},"wordCount":1575,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612025952-Tree%20Traversal.jpg","articleSection":["Trees"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/","url":"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/","name":"Tree Traversal : Inorder, Preorder and Postorder","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612025952-Tree%20Traversal.jpg","datePublished":"2023-02-17T12:11:04+00:00","dateModified":"2023-11-01T12:02:51+00:00","description":"Tree Traversal is the process of travelling across all the nodes in a tree data structure such that no node is left unvisited in the path.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612025952-Tree%20Traversal.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676612025952-Tree%20Traversal.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/tree-traversal-inorder-preorder-postorder\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Trees","item":"https:\/\/prepbytes.com\/blog\/category\/tree\/"},{"@type":"ListItem","position":3,"name":"Tree Traversal : Inorder, Preorder and Postorder"}]},{"@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\/13281","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=13281"}],"version-history":[{"count":2,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/13281\/revisions"}],"predecessor-version":[{"id":18299,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/13281\/revisions\/18299"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=13281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=13281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=13281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}