Data Structures & AlgorithmsModerateGATE CS · ICPC regional

A tree has vertices with values in brackets: M[0], R[5], A[9], G[-4], H[9]. The edges are M–R, M–A, R–G, M–H. A path is any sequence of adjacent vertices with no repeated vertex (a single vertex is a path). What is the maximum possible sum of values along one path?

  1. A. 9
  2. B. 19
  3. C. 18
  4. D. 23

Correct answer: C. 18

A DP that combines the two best downward chains at each vertex finds 18. Paths restricted to a single ancestor-to-descendant line give at most 9, and paths that start at the root give 9.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic