in DS
1,927 views
0 votes
0 votes

Consider the following function foobar(), which takes 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 node of binary tree.

$B)$ Number of leaves in binary tree

$C)$ Sum of leaves node of binary tree.

$D)$ None


What return $10$ actually means?

in DS
by
1.9k views

1 comment

Maybe it's a type they meant 1. For this assumption B is correct.

How to think? We can see that value is incremented only when both left and right subtree is empty. So B.
0
0

1 Answer

0 votes
0 votes
The program is calculating the number of leaf nodes in the BST.

Returning 10 means 10 times of leaf node.

So answer is B.

1 comment

is it? elaborate plz
0
0

Related questions

2 votes
2 votes
3 answers
3