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
Examples
01 · Example 1
n = 4 k = 0 edges = [[1, 2], [2, 3], [4, 3]] return = 3
:3
02 · Example 2
n = 4 k = 1 edges = [[2, 3], [4, 3], [1, 4]] return = 2
:o
More Google problems
- Consolidate On-Call RotationsOA · Seen Jun 2026
- Detonate Bombs with Chain ReactionsONSITE INTERVIEW · Seen May 2026
- Evaluate a Nested Math ExpressionONSITE INTERVIEW · Seen May 2026
- Tic-Tac-Toe Game StatusPHONE SCREEN · Seen May 2026
- Longest Dictionary TokenizationPHONE SCREEN · Seen May 2026
- Minimum Cars for Rental RequestsONSITE INTERVIEW · Seen Apr 2026
- Shortest Path with Mandatory WaypointONSITE INTERVIEW · Seen Apr 2026
- Count Divisible Coin SelectionsOA · Seen Dec 2025
public int findMinimumDiameter(int n, int k, int[][] edges) {
// write your code here
}
n4
k0
edges[[1, 2], [2, 3], [4, 3]]
expected3
sign in to submit