Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 D

Problem: In a binary tree with 7 nodes in level order [10, 5, 15, 2, 7, 12, 20] (missing children are shown as absent, so the array encodes a complete tree), compute the sum over all edges of the smaller of the two subtree sizes below that edge.

  1. A. 32
  2. B. 17
  3. C. 10
  4. D. 6

Correct answer: C. 10

Cutting a root edge leaves a 3-node subtree (smaller side 3) and cutting each of the four leaf edges leaves 1, so the sum is 3 + 3 + 1 + 1 + 1 + 1 = 10. Taking the larger side gives 4 + 4 + 6·4 = 32, summing every node's subtree size including the root gives 17, and 6 just counts edges.

More Coding Interview Problems questions

Exams that ask this topic