Skip to content
Official PYQ · 2014

GATE CS 2014 Set 1 Q12 — Trees

Explain with AI
Consider a rooted n node binary tree represented using pointers. The best upper bound on the time required to determine the number of subtrees having exactly 44 nodes is O(nalog⁡bn)O(n^a\log^bn). Then the value of a+10ba+10b is __________.
View worked solution

Answer: 1

To find the number of subtrees having exactly 44 nodes in a rooted binary tree of nn nodes, we can use a bottom-up (post-order) traversal approach.

For any node uu in the binary tree, the size of the subtree rooted at uu, denoted by S(u)S(u), is given by:

S(u)=1+S(u.left)+S(u.right)S(u) = 1 + S(u.\text{left}) + S(u.\text{right})

where the size of a non-existent child (null pointer) is 00.

Algorithm:

  1. Perform a post-order traversal of the tree.
  2. For each node, compute its subtree size in O(1)O(1) time using the sizes of its left and right children (which are already computed due to the post-order nature of the traversal).
  3. Maintain a global counter. If S(u)=4S(u) = 4, increment the counter.

Complexity Analysis:

  • Since each node is visited exactly once and we perform a constant number of operations at each node, the total time complexity is Θ(n)\Theta(n).
  • Thus, the best upper bound on the time required is O(n)O(n).

We are given that the upper bound is O(nalog⁡bn)O(n^a \log^b n). Comparing this with O(n1log⁡0n)O(n^1 \log^0 n):

a=1andb=0a = 1 \quad \text{and} \quad b = 0

We need to find the value of a+10ba + 10b:

a+10b=1+10(0)=1a + 10b = 1 + 10(0) = 1

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

Published
Report a correction
Keep exploringMore Trees questions