Question

Consider a binary tree TT that has 200 leaf nodes. Then, the number of nodes in TT that have exactly two children are _____.

Official answer and solution
Official answer
199
From
Official GATE 2015 answer key

Solution

Apply the fundamental binary tree relation between the number of leaves and the number of degree-2 nodes.

In any binary tree TT:

  • Let n0n_0 be the number of leaf nodes (nodes with 0 children).
  • Let n1n_1 be the number of nodes with exactly 1 child.
  • Let n2n_2 be the number of nodes with exactly 2 children.
  • Let nn be the total number of nodes: n=n0+n1+n2n = n_0 + n_1 + n_2.

Every node except the root has exactly one incoming edge, so the total number of edges is:

e=n−1=n0+n1+n2−1e = n - 1 = n_0 + n_1 + n_2 - 1

Alternatively, counting edges by the outgoing degree of each parent node:

e=0⋅n0+1⋅n1+2⋅n2=n1+2n2e = 0 \cdot n_0 + 1 \cdot n_1 + 2 \cdot n_2 = n_1 + 2n_2

Equating the two expressions for ee:

n0+n1+n2−1=n1+2n2n_0 + n_1 + n_2 - 1 = n_1 + 2n_2
n0−1=n2  ⟹  n2=n0−1n_0 - 1 = n_2 \implies n_2 = n_0 - 1

Given n0=200n_0 = 200 leaf nodes:

n2=200−1=199n_2 = 200 - 1 = 199

Thus, the number of nodes in TT that have exactly two children is 199.

Final answer

199

Common trap

Assuming the tree must be strictly full or complete, whereas the identity n2 = n0 - 1 holds universally for every binary tree.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2015 Computer Science and Information Technology (CS) Session 3
Question
35 of 65, printed as CS Q.25
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.