GATE Computer ScienceVery hardGATE CS (PYQ pattern)

In a binary tree given as (key, left, right) = (10, (-20, (8), (-6, (4), -)), (15, (-3), (12, (7), (-9)))), a 'path' is any sequence of connected nodes that goes along parent-child edges without repeating a node (it need not pass through the root). What is the maximum possible sum of the keys on a path?

  1. A. 51
  2. B. 40
  3. C. 37
  4. D. 44

Correct answer: D. 44

At every node take the best downward gains (ignoring negative ones) from the two children: the best bend occurs at 15 (left gain 0, right gain 12 + 7 = 19) combined with its parent 10, giving a path 7-12-15-10 of sum 44.

More Programming and Data Structures (GATE) questions

Exams that ask this topic