Consider a rooted \(n\) node binary tree represented using pointers. The best…

2014

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 4 nodes is \(O(n^a\log^bn)\). Then the value of \(a + 10b\) is __________.

Attempted by 129 students.

Show answer & explanation

Correct answer: 1

Key idea: compute the size of the subtree rooted at each node using a single post-order traversal.

  • Step 1: Perform a post-order traversal (process children before the parent).

  • Step 2: For each node, compute subtree_size = 1 + size(left_child) + size(right_child). Store or return this size to the parent.

  • Step 3: Whenever subtree_size equals 4, increment a counter.

Time complexity: each node is visited once and processed in O(1) time (accessing children via pointers and adding sizes), so the overall time is O(n).

Conclusion: the running time is O(n) = O(n^1 log^0 n), so a = 1, b = 0 and a + 10b = 1.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir