GATE / CS ARCHIVE

GATE CSE 2014 SET 1 / QUESTION 12

GATE CSE 2014 Set 1 Q12

Data Structures · Tree Properties & Traversals · Full GATE CSE 2014 Set 1 paper

GATE 2014 Set 1, Q12 1 mark · NAT
Report issue

Tree Properties & Traversals

Question 12, GATE 2014: A rooted binary tree with n nodes is represented by pointers. The best upper bound for counting rooted subtrees containing exactly four nodes is O(nᵃ logᵇ n). Find a+10b.
Tap to enlarge
Answer & solution

Official answer: 1

1. Method and setup. Use one postorder traversal. When processing a node, both child-subtree sizes are already known; combine them with the current node and count it if the total is four. Store size and count in the recursion result rather than recounting descendants for every possible root.

2. Work through the result and its assumptions. A postorder traversal computes each rooted subtree size as 1+left_size+right_size and increments a counter for size 4. It does constant work per node, Θ(n)=O(n¹log⁰n), so a+10b=1.

Original paper, page 10 · Answer key