I am little bit confused with the following code to find height of the tree and the code to find sum of n nos
recursively, which is below this. What does the lheight
and rheight
store, depending on the number of recursions it makes?
int height(struct node* node)
{
if (node==NULL)
return 0;
else
{
lheight = height(node->left);
rheight = height(node->right);
if (lheight > rheight)
return(lheight+1);
else return(rheight+1);
}
}
Here is one more clarification, it prints the sum of n nos
:
int findSum(int n){
int sum;
if(n <= 0) return 0;
else sum = n + findSum(n-1);
return sum;
}
if I change this to:
int findSum(int n){
int sum;
if(n <= 0) return 0;
else sum = findSum(n-1); // here
return sum;
}
It prints the output as 0. Why doesn't this return the number of recursions, if the above tree code does?