GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATA STRUCTURES

Count binary branching nodes from leaves

5 questions solved the same way, newest first. Back to Data Structures

How these are solved

Count edges by child degrees and by total vertices minus one to derive leaves = two-child nodes + 1; substitute given leaf count or, for full trees, total nodes. 1999 requests proof, 2025 disallows one-child nodes and supplies total nodes; retain distinct exercises.

1 mark · MCQ
Report issue

Tree Properties & Traversals

Question 13, GATE 2025: A nonempty binary tree has n nodes, and every node has either zero or exactly two children. How many nodes have two children?
Options: A. (n−2)/2; B. (n−1)/2; C. n/2; D. (n+1)/2.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Let i be the number of internal nodes and l the number of leaves, so n=i+l. Count the tree edges in two ways. Every non-root node has exactly one parent, giving n−1 edges in total. On the other hand, each internal node contributes two parent-to-child edges and each leaf contributes none, giving 2i edges. Therefore 2i=n−1 and i=(n−1)/2.

This also implies l=i+1=(n+1)/2, which explains why D is a plausible but incorrect distractor: it counts leaves. Such a tree has odd n, as n=2i+1. At the boundary n=1, the root is a leaf and the formula gives zero two-child nodes, as required. This is a full/proper binary tree property; it does not require the tree to be balanced or all leaves to be at the same depth. Answer: B.

Original paper, page 12 · Answer key

1 mark · NAT
Report issue

Asked again GATE 2015 Set 3, Q21 (different numbers) · GATE 1995, Q1.17 (different numbers)

Tree Properties & Traversals

Question 23, GATE 2015: A binary tree has 20 leaves. How many nodes have exactly 2 children?
Tap to enlarge
Answer & solution

Official answer: 19

Let n0, n1, n2 count nodes with 0, 1, 2 children. Counting edges gives n1+2n2=n0+n1+n2−1, so n2=n0−1=19, regardless of one-child nodes.

Let L, U, B count leaves, one-child nodes, and two-child nodes. The total nodes are L+U+B, and a tree has one fewer edges. Counting edges by parents instead gives U+2B. Equating U+2B=L+U+B−1 cancels U and gives B=L−1. Hence 20 leaves imply 19 two-child nodes, regardless of how many unary nodes occur.

Original paper, page 34 · Answer key

1 mark · NAT
Report issue

Asked again GATE 2015 Set 2, Q23 (different numbers) · GATE 1995, Q1.17 (different numbers)

Tree Properties & Traversals

Question 21, GATE 2015: A binary tree T has 200 leaf nodes. How many nodes in T have exactly two children? Nodes with exactly one child are allowed.
Tap to enlarge
Answer & solution

Official answer: 199

1. Let n0,n1,n2 count nodes with zero, one and two children. Every non-root node contributes one incoming edge, so E=n0+n1+n2−1. Counting edges from parents instead gives E=n1+2n2; subtracting cancels n1 and yields the result without assuming a full binary tree.

2. Counting tree edges gives n1+2n2=n0+n1+n2−1. Hence the number with two children is n2=n0−1=200−1=199.

Original paper, page 60 · Answer key

3 marks · Descriptive
Report issue

Leaves equal full nodes plus one

Tree Properties & Traversals

Question 12(a), GATE 1999: In a nonempty binary tree, a full node has exactly two children. Prove by induction on height that the number of leaves equals the number of full nodes plus one.
Question 12(a), GATE 1999: In a nonempty binary tree, a full node has exactly two children. Prove by induction on height that the number of leaves equals the number of full nodes plus one.
Tap to enlarge
Shared context

In a nonempty binary tree, a full node has exactly two children. Prove by induction on height that the number of leaves equals the number of full nodes plus one.

Answer & solution

Answer: For every nonempty binary tree, L=F+1, including trees with one-child nodes. (no official key was published)

Use strong induction on the height h, taking a leaf to have height zero. Let L(T) and F(T) count leaves and full nodes in T.

Base h=0: T is a single leaf, so L=1 and F=0, giving L=F+1.

Induction hypothesis: the identity holds for every nonempty binary tree of height less than h. Consider a tree T of height h>0. Its root has one or two children.

If it has one child, the unique child subtree T₁ has smaller height. The root is neither full nor a leaf, so L(T)=L(T₁) and F(T)=F(T₁). By induction L(T)=F(T₁)+1=F(T)+1.

If it has two children with subtrees T₁,T₂, both have smaller height. Here L(T)=L(T₁)+L(T₂) and F(T)=1+F(T₁)+F(T₂), because the root itself is full. Substituting the two induction hypotheses gives L(T)=F(T₁)+1+F(T₂)+1=F(T)+1.

These cases exhaust all roots, proving the claim. The qualification “nonempty” matters: an empty tree has zero leaves and zero full nodes, so the formula would not hold there. It is unnecessary to assume every internal node is full.

Original paper, page 7

1 mark · MCQ
Report issue

Asked again GATE 2015 Set 2, Q23 (different numbers) · GATE 2015 Set 3, Q21 (different numbers)

Leaves and binary branching nodes

Tree Properties & Traversals

Question 1.17, GATE 1995: A nonempty rooted binary tree has n leaves. How many nodes have two children?
Tap to enlarge
Your answer
Answer & solution

Answer: B: n−1 (no official key was published)

Let n0,n1,n2 count nodes with zero, one and two children. The tree has n0+n1+n2−1 edges because it is connected and acyclic. Counting edges by parents gives n1+2n2. Equating the two expressions yields n2=n0−1=n−1. Unary nodes cancel and therefore do not alter the relationship. Here degree two means two children, the rooted-tree convention; interpreting it as undirected graph degree would be a different question.

Original paper, page 2