Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional
A binary tree has these nodes: node 3 (value -7): left none, right none; node 8 (value -7): left 49, right none; node 16 (value 2): left 24, right none; node 24 (value 7): left none, right none; node 30 (value 0): left 96, right 8; node 49 (value 8): left none, right 87; node 54 (value 3): left 92, right none; node 72 (value -9): left none, right 16; node 87 (value -8): left none, right none; node 92 (value 8): left 95, right 99; node 95 (value 8): left none, right none; node 96 (value -6): left 72, right 54; node 99 (value 7): left none, right 3. 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?
- A. 13
- B. 8
- C. 14
- D. 23
Correct answer: D. 23
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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Elements 0 to 8 start as 9 separate singleton sets. The calls union(7, 1), union(5, 2), union(4, 5), union(0, 6), union(0, 2), union(4…
- Elements 0 to 5 start as 6 separate singleton sets. The calls union(3, 0), union(1, 4), union(4, 0), union(5, 5), union(4, 1), union(2…
- Elements 0 to 7 start as 8 separate singleton sets. The calls union(1, 4), union(3, 5), union(1, 3), union(1, 2), union(3, 6), union(6…
- Elements 0 to 7 start as 8 separate singleton sets. The calls union(1, 4), union(7, 1), union(5, 3), union(0, 4), union(4, 1), union(2…
- Elements 0 to 10 start as 11 separate singleton sets. The calls union(6, 0), union(8, 8), union(5, 3), union(2, 1), union(3, 3), union(7…
- Elements 0 to 10 start as 11 separate singleton sets. The calls union(5, 0), union(3, 9), union(9, 3), union(0, 1), union(0, 8), union(6…