Problem · Tree
Find Minimum Possible Diameter
Learn this problemProblem statement
You are given a tree consisting of n vertices.
You can perform the following operation at most k times: delete a single leaf of the tree (the operation can produce new leaves, that can be deleted later). The resulting tree must have as small diameter as possible. Find the minimum possible diameter.
Input Format
The first line contains two space separated integers n and k. Each of the next n-1 lines contains two space separated integers, describing the current tree edge. It's guaranteed that the given graph is a tree.
Constraints
0 < n <= 1e5
0 < k < n
Function
findMinimumDiameter(n: int, k: int, edges: int[][]) → intExamples
Example 1
n = 4k = 0edges = [[1, 2], [2, 3], [4, 3]]return = 3:3
Example 2
n = 4k = 1edges = [[2, 3], [4, 3], [1, 4]]return = 2:o
Constraints
1 <= n <= 10^50 <= k < nedges.length == n - 1- Vertices are numbered from
1ton, andedgesforms a tree.
More Google problems
- Deduplicate Logs: Keep FirstONSITE INTERVIEW · Seen Jul 2026
- Deduplicate Logs: Keep LatestONSITE INTERVIEW · Seen Jul 2026
- Find a Template Across Binary-Tree LeavesONSITE INTERVIEW · Seen Jul 2026
- Maximum Programmer-Problem MatchingONSITE INTERVIEW · Seen Jul 2026
- Minimum Direction ViolationsONSITE INTERVIEW · Seen Jul 2026
- Stream Latest Log VersionsONSITE INTERVIEW · Seen Jul 2026
- Stream Unique Logs in Timestamp OrderONSITE INTERVIEW · Seen Jul 2026
- Top-K IP Addresses from File RecordsONSITE INTERVIEW · Seen Jul 2026