Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional

A binary tree has these nodes: node 18 (value -5): left 23, right 68; node 23 (value 7): left 24, right none; node 24 (value -7): left 66, right none; node 33 (value 6): left none, right none; node 38 (value -1): left none, right none; node 43 (value 1): left 38, right none; node 51 (value 0): left 33, right none; node 64 (value 4): left 43, right 18; node 66 (value 7): left none, right none; node 68 (value 4): left 84, right 51; node 82 (value -6): left none, right none; node 84 (value 3): left 82, right none. 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. 7
  3. C. 10
  4. D. 13

Correct answer: D. 13

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