I made a code to count the total number of nodes in a binary tree.
It simply traverses the tree using inorder traversal and counts all the nodes.Code:int count(struct node *root)
{
static int i=0;
if(root!=NULL)
{
count(root->left);
i++;
count(root->right);
}
return i;
}
I also found a code somewhere which is like this:
I want to know which one will be more efficent(time and space). What is the "O" notation for both of the codes?Code:int tree_size(struct node* node)
{
if (node==NULL)
{
return(0);
}
else
{
return(tree_size(node->left) + tree_size(node->right) + 1);
}
}
I'm a little weak to find "O" notation, that's why I'm asking for it.