|
Re: algorithm to find out height and no of nodes in a binary tree [message #104023 is a reply to message #103915] |
Wed, 30 June 2004 18:05 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
Jai Vrat Singh
Messages: 205 Registered: September 2002 Location: Singapore
|
Senior Member |
|
|
Hi -- it is somwwhat like this ( I HAVE NOT TESTED THE CODE):
int getNodes( struct node* n){
if(n->left == NULL && n->right == NULL)
return(0);
else
return(1+ getNodes(n->left) + getNodes(n->right));
}
int getHeight( struct node* n){
int l_height=0;
int r_height=0;
if(n->left == NULL && n->right == NULL)
return(0);
else{
l_height = getHeight( n->left);
r_heigth = getHeight( n->right);
return(1+ (l_height> r_height)?l_height:r_height);
}
}
|
|
|
|