Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional

Problem: For the binary tree with root value 2 and children: 2 has left child 3 and right child 9; 3 has left child 1 and right child 5; 9 has right child 7; 5 has left child 4. Using the max-path-sum definition (a path may start and end at any node, go up at most once), what is the maximum path sum?

  1. A. 30
  2. B. 31
  3. C. 18
  4. D. 26

Correct answer: A. 30

All values are positive, so the best path is the longest leaf-to-leaf chain 4 → 5 → 3 → 2 → 9 → 7 = 30. Adding every node (including 1, which would need a branch) gives 31, the best root-to-leaf path is 2 + 9 + 7 = 18, and stopping at 5 instead of 4 gives 26.

More Coding Interview Problems questions

Exams that ask this topic