GateAI
Recursion

GATE CSE 2023 · Q47

MCQ2 marksMedium

Time

Consider the C function 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 foo is called with a pointer to the root node of the given binary tree, what will it print?

GATE CSE 2023 Q47 diagram

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: C) 3 8 16 13 24 50

Recursion

The function foo(p) recursively computes the sum of the subtree rooted at node p (post-order traversal: left child, right child, then the current node), prints this subtree sum, and returns it.

Let us trace the function calls starting from the root node 10:

  1. foo(10) evaluates 10 + foo(5) + foo(11):

    • Calls foo(5), which evaluates 5 + foo(3) + foo(8):

      • Calls foo(3): both children are NULL, so retval = 3 + 0 + 0 = 3. Prints 3 and returns 3.

      • Calls foo(8): both children are NULL, so retval = 8 + 0 + 0 = 8. Prints 8 and returns 8.

      • For node 5, retval = 5 + 3 + 8 = 16. Prints 16 and returns 16.

    • Calls foo(11), which evaluates 11 + foo(NULL) + foo(13):

      • foo(NULL) returns 0.

      • Calls foo(13): both children are NULL, so retval = 13 + 0 + 0 = 13. Prints 13 and returns 13.

      • For node 11, retval = 11 + 0 + 13 = 24. Prints 24 and returns 24.

    • For the root node 10, retval = 10 + 16 + 24 = 50. Prints 50 and returns 50.

The complete sequence printed is 3 8 16 13 24 50, which matches option (C).

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Recursion previous year question is Q47 of the GATE CSE 2023 paper. Find more GATE CSE PYQs on Recursion in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Recursion PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Recursion in GATE CSE

Recursion has 19 GATE CSE PYQs across 15 papers from 2011 to 2026. 10 of them are 2-mark MCQs like this one, and it is the only one in GATE CSE 2023.

How GATE tests Recursion

More from GATE CSE 2023

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2023 · Q47 · MCQ

Consider the C function 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 foo is called with a pointer to the root node of the given binary tree, what will it print?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.