Data Structures & AlgorithmsHardCodeforces Div2 D · ICPC regional

A binary tree has these nodes: node 5 (value -6): left none, right 6; node 6 (value 5): left 33, right none; node 21 (value 8): left 5, right 54; node 33 (value 0): left none, right 49; node 49 (value 1): left 72, right none; node 54 (value 7): left none, right none; node 57 (value 4): left none, right none; node 72 (value 3): left none, right none; node 89 (value -8): left 21, right 57. 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. 7
  2. B. 18
  3. C. 8
  4. D. 11

Correct answer: B. 18

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