Ion OA
Anonymous User
592

You are given an undirected tree and can apply at most koperations on it. In one operation, remove a leaf vertex and its adjacent edge. A leaf vertex is a vertex that is connected to exactly one other vertex. Minimize the tree's diameter by applying the operation as many as k times.

Use the following assumptions:

1)he distance between two vertices is the number f edges in their shortest path.
2)he diameter of a tree is the maximum distance among all pairs of vertices in the tree.

Return the minimum possible diameter after applying at most 'k' operations.

Example :
n=5
k=2
edges = [[1, 2], [1, 4], [2, 3], [2, 5]]

image

One possible way to minimize the tree diameter is to delete nodes [3, 5] as shown below. The resulting diameter is 2 formed by the nodes [2, 1, 4].

image

Function Description
Complete the function treeCut in the editor below.

treeCut has the following parameter(s):

  • int n: the number of vertices in the tree
  • int k: the number of vertices that can be deleted
  • int edges[n-1][2]: a 2d array of connected vertices

Constraints

  • 1<=n<=100
  • 0<=k<n
  • 1<=edges[i][0], edges[i][1]≤ n
Comments (4)