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?

Binary tree with directed edges from parent to child. Root node 10. Node 10 has left child 5 and right child 11. Node 5 has left child 3 and right child 8. Node 11 has no left child and has right child 13. Nodes 3, 8 and 13 are leaves.
  1. (A) 3 8 5 13 11 10
  2. (B) 3 5 8 10 11 13
  3. (C) 3 8 16 13 24 50
  4. (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