Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: For the complete binary tree with 10 nodes stored in heap order (node i has children 2i and 2i+1, root is node 1), what is the sum of the depths of all nodes (root depth 0)?

  1. A. 19
  2. B. 29
  3. C. 22
  4. D. 9

Correct answer: A. 19

Node i has depth ⌊log₂ i⌋: one node at depth 0, two at 1, four at 2 and three at 3, so 0 + 2 + 8 + 9 = 19. Using 1-based depths adds 10 (29), assuming a full last level of four nodes gives 22, and 9 counts edges.

More Coding Interview Problems questions

Exams that ask this topic