Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional
A binary tree has these nodes: node 2 (value 6): left none, right none; node 15 (value 9): left none, right none; node 16 (value -9): left none, right 28; node 28 (value -3): left none, right none; node 31 (value -2): left none, right none; node 34 (value -1): left 31, right none; node 43 (value 7): left 15, right 52; node 44 (value 7): left 81, right none; node 52 (value -3): left 34, right 16; node 61 (value 0): left 43, right 44; node 62 (value -3): left 61, right 2; node 81 (value 7): left none, 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?
- A. 30
- B. 13
- C. 9
- D. 19
Correct answer: A. 30
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
- A binary tree has these nodes: node 2 (value -7): left none, right none; node 9 (value 7): left 34, right 39; node 34 (value -5): left 80…
- A binary tree has these nodes: node 4 (value 9): left 67, right 54; node 8 (value 1): left none, right 11; node 11 (value 9): left none…
- 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…
- 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…