Question

Consider a rooted nn node binary tree represented using pointers. The best upper bound on the time required to determine the number of subtrees having exactly 4 nodes is O(nalog⁡bn)O(n^a \log^b n). Then the value of a+10ba + 10b is ___________.

Official answer and solution
Official answer
1
From
Official GATE 2014 answer key

Solution

Analyze the time complexity of counting nodes in subtrees using post-order traversal.

We can compute the size of every subtree in a rooted binary tree using a standard bottom-up post-order traversal.

For any node uu:

size(u)=1+size(left(u))+size(right(u))\text{size}(u) = 1 + \text{size}(\text{left}(u)) + \text{size}(\text{right}(u))
  • At each node, computing its size takes O(1)O(1) time given the sizes of its left and right children.
  • Checking whether size(u)==4\text{size}(u) == 4 and updating a counter takes O(1)O(1) time.
  • The traversal visits each of the nn nodes exactly once.

Thus, the total time required is Θ(n)=O(n)\Theta(n) = O(n).

Comparing O(n)O(n) with the given form O(nalog⁡bn)O(n^a \log^b n):

a=1,b=0a = 1, \quad b = 0

Evaluating the expression:

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

1

Common trap

Recomputing the subtree size from scratch at each node in O(n)O(n) time, which would lead to an inferior O(n2)O(n^2) bound.

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

Source

Paper
GATE 2014 Computer Science and Information Technology (CS) Session 1
Question
22 of 65, printed as CS Q.12
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.