509 views
0 votes
0 votes

I want longest path from root to leaf. Then which code is correct among Code-1 or Code-2?

 

Code-1)

int tree(Struct node *root){
    int a=0, b=0,c=0;
    if(root==NULL)
    return 0;
    if((root->left==NULL)&&(root->right==NULL))
    return 1;
    a=1+tree(root->left);
    b=1+tree(root->right);
    c=1+max(a,b);
    return c;
}

Code-2)

int tree(Struct node *root){
    int a=0, b=0,c=0;
    if(root==NULL)
    return 0;
    if((root->left==NULL)&&(root->right==NULL))
    return 1;
    a=tree(root->left);
    b=tree(root->right);
    c=1+max(a,b);
    return c;
}

1 Answer

Related questions

0 votes
0 votes
1 answer
2
1 votes
1 votes
1 answer
3
srestha asked May 2, 2019
470 views
A $d-$ary heap is a binary heap, but instead of $2$ children, nodes have $d$ children. A $d-ary$ heap can be represented by $1-D$ array as follows. The root is kept in $A...