The GATE Grind

GATE 2014 CS (CS3) – Question 51

Programming and Data Structures · Trees and Binary Search Trees · 2 marks · Multiple choice

An arbitrary tree uses leftMostChild–rightSibling representation. When its root is passed to DoSomething, what does the returned value count?

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

  1. Internal nodes
  2. Tree height
  3. Nodes without a right sibling
  4. Leaf nodes

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (D) Leaf nodes

Explanation

A node with no leftmost child contributes1, identifying a leaf. Otherwise the function counts leaves in its child list. Recursing through rightSiblingadds counts for the remaining siblings. Thus starting at the root returns the **number of leaves,D**, not internal nodes or tree height.