Data Structures & AlgorithmsVery hardGoogle · Flipkart interview

A COMPLETE binary tree has n nodes. Counting them by comparing the leftmost and rightmost depths at each node and recursing into only one subtree takes how long?

  1. A. O(log² n)
  2. B. O(log n)
  3. C. O(n)
  4. D. O(n log n)

Correct answer: A. O(log² n)

Each depth check walks O(log n) edges, and at every level one of the two subtrees is perfect (its size is 2^h − 1 by formula), so only one recursive call continues. That is O(log n) levels × O(log n) work = O(log² n).

More Coding Interview Problems questions

Exams that ask this topic