Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6723415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:33:58+00:00 2026-05-26T09:33:58+00:00

I was going through the tutorial of binary tree . And I am slightly

  • 0

I was going through the tutorial of binary tree .
And I am slightly stuck in use of recursive function . say for example I need to count no of nodes in a tree

int countNodes( TreeNode *root )    
{   
       // Count the nodes in the binary tree to which  
       // root points, and return the answer.  
    if ( root == NULL )  
       return 0;  // The tree is empty.  It contains no nodes.  
    else
   {  
       int count = 1;   // Start by counting the root.  
       count += countNodes(root->left);  // Add the number of nodes   
                                        //     in the left subtree.   
       count += countNodes(root->right); // Add the number of nodes   
                                        //    in the right subtree.  
       return count;  // Return the total.  
    }  
 }   // end countNodes()

Now my doubt is-> how would it count say root->left->left of right ? or root->right->left->left??
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:33:58+00:00Added an answer on May 26, 2026 at 9:33 am

    With recursive functions, you should think recursively! Here’s how I would think of this function:

    • I start writing the signature of the function, that is

      int countNodes( TreeNode *root ) 
      
    • So first, the cases that are not recursive. For example, if the given tree is NULL, then there are no nodes, so I return 0.

    • Then, I observe that the number of nodes in my tree are the number of nodes of the left sub-tree plus the number of nodes of the right sub-tree plus 1 (the root node). Therefore, I basically call the function for the left and right nodes and add the values adding 1 also.
      • Note that I assume the function already works correctly!

    Why did I do this? Simple, the function is supposed to work on any binary tree right? Well, the left sub-tree of the root node, is in fact a binary tree! The right sub-tree also is a binary tree. So, I can safely assume with the same countNodes functions I can count the nodes of those trees. Once I have them, I just add left+right+1 and I get my result.

    How does the recursive function really work? You could use a pen and paper to follow the algorithm, but in short it is something like this:

    Let’s say you call the function with this tree:

      a
     / \
    b   c
       / \
      d   e
    

    You see the root is not null, so you call the function for the left sub-tree:

    b
    

    and later the right sub-tree

       c
      / \
     d   e
    

    Before calling the right sub-tree though, the left sub-tree needs to be evaluated.

    So, you are in the call of the function with input:

    b
    

    You see that the root is not null, so you call the function for the left sub-tree:

    NULL
    

    which returns 0, and the right sub-tree:

    NULL
    

    which also returns 0. You compute the number of nodes of the tree and it is 0+0+1 = 1.

    Now, you got 1 for the left sub-tree of the original tree which was

    b
    

    and the function gets called for

       c
      / \
     d   e
    

    Here, you call the function again for the left sub-tree

    d
    

    which similar to the case of b returns 1, and then the right sub-tree

    e
    

    which also returns 1 and you evaluate the number of nodes in the tree as 1+1+1 = 3.

    Now, you return the first call of the function and you evaluate the number of nodes in the tree as 1+3+1 = 5.

    So as you can see, for each left and right, you call the function again, and if they had left or right children, the function gets called again and again and each time it goes deeper in the tree. Therefore, root->left->left or root->right->left->left get evaluated not directly, but after subsequent calls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm going through a tutorial to learn how to use Backbone.js and I'm having
I was going through this tutorial for fun, and got stuck on the very
I am going through Generics Tutorial and was going through example to copy objects
I was going through a tutorial about defining functions with an example on creating
I'm starting to learn to use Inkscape, and I'm going through this tutorial on
I'm going through the full tutorial at cplusplus.com, coding and compiling each example manually.
I am going through this tutorial about PDO and have come to the point
I just downloaded MVC and I am going through a tutorial. Everything goes fine
Trying to learn a bit about PDO and is going through this tutorial .
Going through the microsoft authentication tutorial listed here they have you create a master

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.