#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data)
{
struct node* node=(struct node*)malloc(sizeof(struct node));
node->data=data;
node->left=NULL;
node->right=NULL;
return (node);
};
int height(struct node* root)
{
static int lheight,rheight;
if(root==NULL)
return;
else
{
lheight=height(root->left)+1;
rheight=height(root->right)+1;
if(lheight>rheight)
return lheight;
else
return rheight;
}
}
int main()
{
struct node* root=newNode(1);
root->left=newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("%d",height(root));
return 0;
}
This program gives two different result. One is 2 for the above program where I use static and 3 if static is not used. Please explain the reason for the change in output using static.