Skip to content
Official PYQ · 2015

GATE CS 2015 Set 2 Q10 — Trees

Explain with AI
A binary tree T has 2020 leaves. The number of nodes in T having two children is ______.
View worked solution

Answer: 19

In any binary tree, there is a direct relationship between the number of leaf nodes (nodes with degree 0) and the number of nodes with exactly two children.

Let:

  • n0n_0 be the number of leaf nodes (nodes with 00 children)
  • n1n_1 be the number of nodes with 11 child
  • n2n_2 be the number of nodes with 22 children

The total number of nodes in the tree is:

N=n0+n1+n2— (Equation 1)N = n_0 + n_1 + n_2 \quad \text{--- (Equation 1)}

Since every node except the root has exactly one incoming edge, the total number of edges in a tree with NN nodes is N−1N - 1. We can also count the total number of edges by summing the outgoing degrees of all nodes:

Total Edges=0⋅n0+1⋅n1+2⋅n2=n1+2n2\text{Total Edges} = 0 \cdot n_0 + 1 \cdot n_1 + 2 \cdot n_2 = n_1 + 2n_2

Equating the two expressions for the total number of edges:

N−1=n1+2n2— (Equation 2)N - 1 = n_1 + 2n_2 \quad \text{--- (Equation 2)}

Substitute Equation 1 into Equation 2:

(n0+n1+n2)−1=n1+2n2(n_0 + n_1 + n_2) - 1 = n_1 + 2n_2

Simplifying the equation by subtracting n1+n2n_1 + n_2 from both sides:

n0−1=n2n_0 - 1 = n_2n2=n0−1n_2 = n_0 - 1

Given that the number of leaves n0=20n_0 = 20:

n2=20−1=19n_2 = 20 - 1 = 19

Thus, the number of nodes in TT having two children is 19.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Trees questions