edited by
18,843 views
33 33 votes

Consider the $\text{C}$ function $\text{foo}$ and the binary tree shown.

typedef struct node {
    int val;
    struct node *left, *right;
} node;

int foo(node *p) {
    int retval;
    if (p == NULL)
        return 0;
    else {
        retval = p->val + foo(p->left) + foo(p->right);
        printf("%d ", retval);
        return retval;
    }
}

When $\textsf{foo}$ is called with a pointer to the root node of the given binary tree, what will it print?

  1. $3 \;8 \;5 \;13 \;11\; 10$
  2. $3 \;5\; 8\; 10\; 11\; 13$
  3. $3 \;8 \;16 \;13\; 24\; 50$
  4. $3\; 16\; 8\; 50\; 24\; 13$

6 Answers

15 15 votes

Given that retval = p → val + foo(p → left) + foo(p → right)

Means it will print the sum of ( value + leftsubtree whole value + right subtree whole value ).

Note that, until the child nodes completes the calculation, parent node calculation step will not complete.

 

Therefore leaf nodes returns the value of leaf nodes alone. ( 3 node, 8 node and 13 node will return 3, 8 and 13 respectively )

5 node will return 5+3+8 = 16

11 node will return 11+13 = 24

10 node will return 10+16+24 = 50

 

So sequence 3,8,16,13,24,50 only possible in the given options

 

As we know, there’s no rule about evaluation order of parameters of ‘+’

possible outcomes : 

  1. 3,8,16,13,24,50
  2. 8,3,16,13,24,50
  3. 13,24,3,8,16,50
  4. 13,24,8,3,16,50
4 4 votes

The function is taking the current node value and calling the function on the left and right child nodes. Recursively, it will reach the leftmost leaf node. The value for the leftmost leaf node is:

retval = 3 + 0 + 0,

and it prints '3'. The value '3' is returned to its parent node.

Now, the value at node '5' is:

retval = 5 + 3 + foo(p->right).

Similarly, foo(p->right) will print and return its value, '8', to the parent node.

Now, retval = 5 + 3 + 8. So, the output '16' is printed and returned to its parent node, '10'. By this time, you should have an intuition of what's happening in the function. This process will continue recursively.

The final output is: 3, 8, 16, 13, 24, 50.

Answer: Option C.

 

 

For tracking purpose,I'm attaching the image

0 0 votes

The function is taking the current node value and calling the function on the left and right child nodes. Recursively, it will reach the leftmost leaf node. The value for the leftmost leaf node is:

retval = 3 + 0 + 0,

and it prints '3'. The value '3' is returned to its parent node.

Now, the value at node '5' is:

retval = 5 + 3 + foo(p->right).

Similarly, foo(p->right) will print and return its value, '8', to the parent node.

Now, retval = 5 + 3 + 8. So, the output '16' is printed and returned to its parent node, '10'. By this time, you should have an intuition of what's happening in the function. This process will continue recursively.

The final output is: 3, 8, 16, 13, 24, 50.

Answer: Option C.

0 0 votes

basically it's adding the values of left child + right child + root  and doing this process from bottom to top of the tree. just check the final sum i.e 50 and it matches option C. (no need to dry run the whole program )

Answer:
Position:
Show:

Related questions

26 26 votes
3 3 answers
16.8k
16.8k views
admin asked Feb 15, 2023
16,758 views
Let $A$ be a priority queue for maintaining a set of elements. Suppose $A$ is implemented using a max-heap data structure. The operation $\text{EXTRACT-MAX} (A)$ extracts...
23 23 votes
4 4 answers
15.8k
15.8k views
admin asked Feb 15, 2023
15,787 views
Consider a sequence $a$ of elements $a_{0}=1, a_{1}=5, a_{2}=7, a_{3}=8, a_{4}=9$, and $a_{5}=2$. The following operations are performed on a stack $S$ and a queue $Q,$ b...
30 30 votes
3 3 answers
14.0k
14.0k views
admin asked Feb 15, 2023
14,038 views
Which one of the following sequences when stored in an array at locations $A , \ldots, A[10]$ forms a max-heap?$23,17,10,6,13,14,1,5,7,12$$23,17,14,7,13,10,1,5,6,12$$23,1...
40 40 votes
6 6 answers
27.0k
27.0k views
admin asked Feb 15, 2023
26,966 views
Let $\textsf{SLLdel}$ be a function that deletes a node in a singly-linked list given a pointer to the node and a pointer to the head of the list. Similarly, let $\textsf...