GATE / CS ARCHIVE

GATE CSE 2014 SET 3 / QUESTION 41

GATE CSE 2014 Set 3 Q41

Data Structures · Tree Properties & Traversals · Full GATE CSE 2014 Set 3 paper

GATE 2014 Set 3, Q41 2 marks · MCQ
Report issue

Tree Properties & Traversals

Question 41, GATE 2014: An arbitrary tree uses leftMostChild/rightSibling pointers. Function D(t): set value=0; if t is non-NULL, set value=1 when t.leftMostChild is NULL, otherwise set value=D(t.leftMostChild); then add D(t.rightSibling); return value. Passing the root returns what? A internal-node count; B tree height; C number of nodes without right siblings; D leaf count.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Step 1: A NULL pointer returns zero. A node with no child contributes one for itself, identifying a leaf. A node with children contributes only the recursive count from its child list, not one for itself. Step 2: The rightSibling recursion adds the counts of the remaining sibling subtrees. It does not test whether a sibling exists as a property to count. Step 3: Inductively each forest call counts exactly its leaves; at the root, whose sibling pointer is NULL, this is the entire tree's leaf count, D. The recurrence adds branches rather than taking a maximum, so it does not compute height.

Original paper, page 62 · Answer key