Thursday, 2 August 2012

C function to compute the maximum depth in a tree

int maxDepth(struct node* root) 
{ 

int leftDepth = 0;
int rightDepth = 0;
if (root==NULL) 
{ 
return 0; 
} 
else 
{ 
leftDepth = maxDepth(root->left); 
rightDepth = maxDepth(root->right); 
if (leftDepth > rightDepth)

              return(leftDepth+1); 
else

              return(rightDepth+1); 
} 
}

No comments:

Post a Comment