Data Structures & AlgorithmsHardProduct Company SDE · ICPC regional

Problem: The house-robber rule applies on a tree: you pick a set of nodes with no parent–child pair both chosen, maximising the total value. Tree: root r=1 (value 3), children 2 (value 4) and 3 (value 5); node 2 has children 4 (value 1) and 5 (value 6); node 3 has child 6 (value 2). What is the maximum total value?

  1. A. 12
  2. B. 9
  3. C. 21
  4. D. 15

Correct answer: A. 12

With (take, skip) per node, the best is 12, reached both by the root 3 with nodes 4, 5, 6 (1 + 6 + 2) and by 5 + 6 + 1 without the root. Taking only the middle level gives 9, ignoring the constraint gives 21, and 4 + 5 + 6 = 15 forgets that node 5 is node 2's child.

More Coding Interview Problems questions

Exams that ask this topic