Question

A binary tree TT has 20 leaves. The number of nodes in TT having two children is _____.

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

Solution

Apply the structural relation between leaves and degree-2 internal nodes in a binary tree.

Let nn be the total number of nodes in binary tree TT, and let n0,n1,n2n_0, n_1, n_2 denote the number of nodes with 0, 1, and 2 children, respectively.

The total number of nodes is:

n=n0+n1+n2.n = n_0 + n_1 + n_2.

Every node except the root has exactly one incoming edge, so the total number of edges is n−1n - 1. The number of edges can also be counted from outgoing degrees:

n−1=0⋅n0+1⋅n1+2⋅n2=n1+2n2.n - 1 = 0 \cdot n_0 + 1 \cdot n_1 + 2 \cdot n_2 = n_1 + 2n_2.

Subtracting the two equations:

(n0+n1+n2)−1=n1+2n2  ⟹  n0−1=n2  ⟹  n2=n0−1.(n_0 + n_1 + n_2) - 1 = n_1 + 2n_2 \implies n_0 - 1 = n_2 \implies n_2 = n_0 - 1.

Given n0=20n_0 = 20 leaves:

n2=20−1=19.n_2 = 20 - 1 = 19.
Final answer

19

Common trap

Confusing the relation for general binary trees (n0=n2+1n_0 = n_2 + 1) with that for strictly full binary trees.

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 2
Question
20 of 65, printed as CS Q.10
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.