GATE 2014 CS (CS3) – Question 51
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;
}
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.