Question
Consider a rooted 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 . Then the value of 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 :
- At each node, computing its size takes time given the sizes of its left and right children.
- Checking whether and updating a counter takes time.
- The traversal visits each of the nodes exactly once.
Thus, the total time required is .
Comparing with the given form :
Evaluating the expression:
1
Recomputing the subtree size from scratch at each node in time, which would lead to an inferior 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.