GateAI
Trees

GATE CSE 2014 Set 3 · Q41

MCQ2 marksMedium

Time

Consider the pseudocode given below. The function DoSomething() takes as argument a pointer to the root of an arbitrary tree represented by the leftMostChild-rightSibling representation. Each node of the tree is of type treeNode.typedef struct treeNode* treeptr; struct treeNode { treeptr leftMostChild, rightSibling; }; int DoSomething (treeptr tree) { int value=0; if (tree != NULL) { if (tree->leftMostChild == NULL) value = 1; else value = DoSomething(tree->leftMostChild); value = value + DoSomething(tree->rightSibling); } return(value); }When the pointer to the root of a tree is passed as the argument to DoSomething, the value returned by the function corresponds to the

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: D) number of leaf nodes in the tree.

Trees

In the leftMostChild-rightSibling representation of an arbitrary tree:

  1. A node's leftMostChild pointer points to its first child in the original tree.

  2. A node's rightSibling pointer points to the immediate next sibling among the children of the same parent.

A node in the arbitrary tree is a leaf node if and only if it has no children, which means tree->leftMostChild == NULL.

Let us trace the function DoSomething(tree):

  • If tree == NULL, it returns 0.

  • If tree != NULL:

    • If tree->leftMostChild == NULL, the current node has no children (it is a leaf node in the original arbitrary tree), so value is initialized to 1.

    • Otherwise, value recursively computes the number of leaves in the subtree rooted at its children by calling DoSomething(tree->leftMostChild).

    • Then, it adds DoSomething(tree->rightSibling) to recursively count leaves in all its siblings.

Thus, the function traverses the entire tree and counts every node that has leftMostChild == NULL, which is the total number of leaf nodes in the tree.

Hence, option (D) is correct.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Trees previous year question is Q41 of the GATE CSE 2014 Set 3 paper. Find more GATE CSE PYQs on Trees in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Trees PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Trees in GATE CSE

Trees has 13 GATE CSE PYQs across 11 papers from 2012 to 2026. 3 of them are 2-mark MCQs like this one, and GATE CSE 2014 Set 3 has 2.

How GATE tests Trees

More from GATE CSE 2014 Set 3

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2014 Set 3 · Q41 · MCQ

Consider the pseudocode given below. The function DoSomething() takes as argument a pointer to the root of an arbitrary tree represented by the leftMostChild-rightSibling representation. Each node of the tree is of type treeNode . typedef struct treeNode* treeptr; struct treeNode { treeptr leftMostChild, rightSibling; }; int DoSomething (treeptr tree) { int value=0; if (tree != NULL) { if (tree->leftMostChild == NULL) value = 1; else value = DoSomething(tree->leftMostChild); value = value + DoSomething(tree->rightSibling); } return(value); } When the pointer to the root of a tree is passed as the argument to DoSomething , the value returned by the function corresponds to the

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.