GATE CS 2023 Set 1 — Question 47
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.MCQ+2 / -0.67MediumFunctions & RecursionC ProgrammingProgramming & Data StructuresTree TraversalsTrees
Programming & Data Structures → C Programming → Functions & Recursion
Last updated
Question
Consider the C function
When
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;
}
}

foo is called with a pointer to the root node of the given binary tree, what will it print?Correct answer
(C) 3 8 16 13 24 50
Solution
The function
foo calculates the sum of all node values in the subtree rooted at p. It uses a post-order traversal (Left, Right, Root) because it recursively calls itself on the left and right children before calculating retval and printing it.Let's trace the execution:1.
foo(3): retval = 3 + 0 + 0 = 3. Prints "3 ". Returns 3.2.
foo(8): retval = 8 + 0 + 0 = 8. Prints "8 ". Returns 8.3.
foo(5): retval = 5 + foo(3) + foo(8) = 5 + 3 + 8 = 16. Prints "16 ". Returns 16.4.
foo(13): retval = 13 + 0 + 0 = 13. Prints "13 ". Returns 13.5.
foo(11): retval = 11 + foo(NULL) + foo(13) = 11 + 0 + 13 = 24. Prints "24 ". Returns 24.6.
The sequence of printed values is: 3 8 16 13 24 50.foo(10): retval = 10 + foo(5) + foo(11) = 10 + 16 + 24 = 50. Prints "50 ". Returns 50.Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on C Programming
2026 Set 2 Q12The set T represents various traversals over binary tree. The set S represents the order of…2026 Set 2 Q19Consider the following three ANSI-C programs, P1, P2, and P3. P1 [code] P2 [code] **P3**…2026 Set 1 Q23Let be an odd number greater than 100. Consider a binary minheap with elements stored in an…2026 Set 1 Q24Consider a hash table that is initially empty. The hash table is maintained…2026 Set 1 Q27Consider the following C statements: Which of the following options is/are correct? [figure]