GATE Assistant

Past questions › Computer Science and Information Technology › GATE 2023

GATE 2023 · CS · MCQ · 2 marks · Programming and Data Structures › Recursion

GATE 2023 Computer Science and Information Technology question: Consider the C function foo and the binary tree shown. code When foo is called with a…

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;
  }
}

figure

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

  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

Show answer

Official answer: C

Practise this question with exam-style marking

Discussion

No discussion yet.

Join the discussion