Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional

A binary tree has these nodes: node 2 (value 5): left none, right 81; node 13 (value -5): left 41, right 28; node 23 (value -3): left none, right none; node 28 (value -1): left 90, right 31; node 30 (value 2): left 61, right none; node 31 (value 9): left none, right none; node 41 (value -3): left none, right 51; node 51 (value -6): left 23, right 2; node 61 (value 1): left none, right none; node 81 (value 6): left none, right none; node 90 (value 8): left none, right 30. A path is any sequence of nodes in which consecutive nodes are joined by an edge, with no node repeated; a single node is a path. The value of a path is the sum of its node values. What is the maximum value of any path?

  1. A. 5
  2. B. 9
  3. C. 19
  4. D. 7

Correct answer: C. 19

The best path may bend at any node, start and end anywhere, and can be a single node. Restricting it to start at the root, to be one node, or to pass through the root gives smaller values.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic