Data Structures & AlgorithmsVery hardProduct company OA

Each node of this binary tree holds a value; how many DOWNWARD paths (starting at any node, moving only to children, ending at any node) have a sum of exactly 8? Tree (parent: left, right): 10: 5, −3; 5: 3, 2; −3: –, 11; 3: 3, −2; 2: –, 1. Use the prefix-sum + hash-map method.

  1. A. 3
  2. B. 2
  3. C. 4
  4. D. 1

Correct answer: A. 3

Valid paths: 5 → 3, 5 → 2 → 1, and −3 → 11. The prefix-sum map counts, at each node, how many ancestors' running sums equal (current sum − 8). Counting only root-to-node paths finds none of them (they sum to 10, 15, 18, 21, 13, 17, 7, 18), and 3 → 3 + … paths reach 6 or 1, not 8.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic