GATE CS 2023 · Question 47 · Programming and Data Structures · Multiple choice (MCQ) · 2 marks · −2/3 for a wrong answer
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?

- (A) 3 8 5 13 11 10
- (B) 3 5 8 10 11 13
- (C) 3 8 16 13 24 50
- (D) 3 16 8 50 24 13
Answer
Answer (C)
Answer from the official GATE CS 2023 answer key.
The step-by-step solution, where available, is part of GATE CS Pro. See what Pro includes