Write a program to Calculate Size of a tree

Size of a tree is the number of elements present in the tree.

Code :
Below code is recursive way of finding size .

/* function to return number of nodes in a tree. */
int size(struct node* node)
{
  if (node==NULL)
    return 0;
  else
    return(size(node->left) + 1 + size(node->right));
}

Time & Space Complexities: Since this program is similar to traversal of tree, time and space complexities will be same as Tree traversal .

Leave a Reply

Your email address will not be published. Required fields are marked *

For Inserting code :
Paste your code in the comment form, select it and then click the language link

C | C++ | Java |

*