Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon OA

Problem: A binary tree in level order (None for absent nodes) is [5, 4, 8, 11, None, 13, 4, 7, 2, None, None, 5, 1]. How many root-to-leaf paths have values summing to exactly 22?

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

Correct answer: A. 1

Depth-first search carrying the running sum; a path counts only when it ends at a leaf with the target sum. Counting every node whose prefix sum equals the target counts non-leaf endpoints too.

More Coding Interview Problems questions

Exams that ask this topic