Friday, Jan 19th

Last update12:59:40 PM GMT

Write a C program to determine the number of elements (or size) in a tree.

Write e-mail

Once again, a simple problem which can be solved in recursive manner. Start with the root element, and call the recursive method on left and right subtrees. The result will be returned by adding number of elements in left sub-tree, number of elements in right sub-tree and adding 1.

argaiv1077

int tree_size(struct node* node) 
{ 
 if (node==NULL) 
 { 
 return(0); 
 } 
 else 
 { 
 return(tree_size(node->left) + tree_size(node->right) + 1); 
 } 
}

Share this post



Web Hosting