All problems
Reroot DP Max Distance
Problem
Compute the eccentricity of every node of a tree with the rerooting technique.
First compute downward heights with a post-order pass, then propagate best distances from the parent side using the two largest child heights. Return the maximum distance from each node. edges is a list of [u, v] pairs; the tree is rooted at 0 internally.
Hint
Each node uses the best child path plus the best path through its parent.
Test cases
case 1
in: [4,[[0,1],[1,2],[2,3]]]
expected: [3,2,2,3]
case 2
in: [4,[[0,1],[0,2],[0,3]]]
expected: [1,2,2,2]
case 3
in: [1,[]]
expected: [0]
case 4
in: [2,[[0,1]]]
expected: [1,1]
Starter codePython
def reroot_max_distance(n, edges):
# Your code here
pass4 test cases · run your code against them in the editor