edited by
1,484 views
0 votes
0 votes
Consider the following functions foobar(), which takes a binary tree as input:

int foobar (struct node * root)

{

     if(!root) return 0;

     if(!root → left && !root → right) return 10;

     else

     {

           int i = foobar (root → left);

           int j = foobar (root → right);

           return (i+j);

     }

}

What does the above function foobar compute ?

(A) sum of internal nodes of the binary tree

(B) number of leaves of the binary tree

(C) sum of leaves of the binary tree

(D) none of these
edited by

1 Answer

1 votes
1 votes

For each node having (root → left == NULL && root → right == NULL) i.e. LEAF NODE program returns 10.

So the value foobar computes is 10*(number of leaves of the binary tree)

Hence, D is the correct answer.

In a similar fashion, if you replace return(i+j) with return(i+j+$A$) it will compute $A$*(number of internal nodes of the binary tree)

where, $A$ is any constant

Related questions

0 votes
0 votes
0 answers
2
BOB asked Dec 17, 2018
330 views
Consider the integer array A[1…...100, 1…...100] in which the elements are stored in Z representation. If the base address of A is starting from 1000 onwards, size of...