Wildlife Corridor Renewal Atlas

Minimum achievable tree diameter after replacing each edge

HardTreesRerootingTree Diameter

A wildlife reserve has survey stations connected by corridors forming a tree. Every corridor takes one unit of time to traverse.

The stations are numbered from 0 to n - 1. The tree is described by parents, where n = len(parents) + 1: for each station v from 1 to n - 1, there is a corridor between v and parents[v - 1]. Each parent has a smaller station number than its child.

The reserve is considering a separate renewal experiment for each existing corridor:

  1. Remove that corridor, splitting the tree into two components.
  2. Build exactly one new corridor between any station in one component and any station in the other component. The new corridor also takes one unit of time to traverse. Rebuilding the original corridor is allowed.

The diameter of a tree is the largest distance between any two of its stations, where distance is the number of corridors on their unique connecting path. A single-station tree has diameter zero.

Return an integer array answer of length n - 1. For every v from 1 to n - 1, answer[v - 1] must be the smallest possible diameter after renewing the corridor between v and parents[v - 1].

Each experiment starts from the original tree; changes from one experiment do not carry over to another. If there are no corridors, return an empty array.

Examples

Example 1

Input: parents = [0,1,2,3]
Output: [3,3,3,3]

The stations form a chain. Each removal leaves two shorter chains. For that experiment, consider connecting a middle station of one chain to a middle station of the other, rather than necessarily rebuilding the removed corridor. Results are listed in the order of the child stations.

Example 2

Input: parents = [0,0,0,0]
Output: [2,2,2,2]

All four corridors meet at station 0. Removing any corridor isolates one leaf. Reconnecting that leaf to station 0 is permitted, so each experiment can retain the original tree's shape.

Constraints

  • 0 <= parents.length <= 99999
  • n = parents.length + 1
  • For every v with 1 <= v < n, 0 <= parents[v - 1] < v
  • Every original and replacement corridor has length 1

The intended solution uses O(n) time and O(n) space. For component diameters a and b, the best reconnected diameter is max(a, b, ceil(a / 2) + ceil(b / 2) + 1). Iterative passes avoid recursion-depth issues on long chains.

Hints

Show hint 1

If the two components have diameters a and b, what is the best possible maximum distance across a new corridor? Relate each component's smallest possible maximum distance from a station to its diameter.

Show hint 2

Compute downward heights and diameters first. In a second pass, propagate the height and diameter outside each subtree. Keeping only the three largest branch heights and two largest component diameters lets you exclude any one child in constant time.

Follow-up questions

What an interviewer might ask once you have a working solution.

  • How would you also return endpoints of an optimal replacement corridor for every experiment, breaking ties lexicographically?
  • How would the reconnection formula change if the replacement corridor had a given positive integer length instead of length 1?

Practice this with an AI interviewer

Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.

Start this problem