Consider the C function foo and the binary tree shown. typedef struct node {…

GATE · 2023 · CS · Computer Science & IT

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?

  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

Attempted by 238 students.

Show answer

Correct answer: C

The worked solution is available to enrolled students.

Explore the full course: Iocl Engineers Officers Grade A Paper 2

Loading lesson…