depth of a tree

i have come across this code for calculating the depth of a binary tree.

int maxDepth(Node *&temp)
{
if(temp == NULL)
return 0;

else
{
int lchild = maxDepth(temp->left);
int rchild = maxDepth(temp->right);

if(lchild <= rchild) 1
return rchild+1; 2

else 3
return lchild+1; 4

}
}


in these recursive calls i am really clue less about how the statements i numbered from 1 to 4 would be executed...in every recursive call...???


lets temp has left depth of 3 and right depth of 100 then...in last 97 recursive calls max(temp->left) would be doing...????? how these recursive mechanism work here...plz dont answer this vaguely....i want to know specifically how these left node and right node recursive calls are co-ordinating with each other????
Plz explain...


Thanks
my exaxt question is ...how it is dealt when we make two recursive calls as we did here (int lchild = maxDepth(temp->left);
int rchild = maxDepth(temp->right )those require different number of transactions... to compute????


????
All left recursive calls happen before right recursive calls. Once you go as far down left as you can, that deepest left goes right.

It's hard to tell what you're asking. In the lines numbered 1-4, it's literally just returning the larger number of the two - it's the same as saying
return max(lchild, rchild) + 1;
Last edited on
Topic archived. No new replies allowed.