Data Structures & AlgorithmsModerateCodeforces Div2 D · ICPC regional

A binary tree has these nodes: node 1 (value -5): left 30, right 56; node 10 (value -1): left none, right none; node 20 (value 5): left none, right none; node 27 (value 1): left none, right none; node 30 (value 8): left none, right 27; node 56 (value 5): left none, right 10; node 83 (value -2): left 1, right 20. 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. 9
  2. B. 3
  3. C. 8
  4. D. 7

Correct answer: A. 9

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