Tree Cutting

Given a tree with n nodes. You must delete exactly k edges to break the tree into k+1 separate pieces (components).

Your task is to make the smallest piece as large as possible.

Output the maximum possible size of the smallest piece.

The edges array is a 2D array of [u, v] pairs representing the undirected edges of the tree. Use this to build your 1-indexed adjacency list.

Example 1:

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

Output: 2

Explanation: We need to make 1 cut to form 2 components. If we cut the edge between 1 and 3, we get one component with nodes {1, 2} (size 2) and another with nodes {3, 4, 5} (size 3). The minimum size is 2. We cannot achieve a minimum size of 3, so 2 is the maximum possible answer.

Example 2:

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

Output: 1

Explanation: No matter which 2 edges you cut, the isolated leaf nodes will always have a size of 1.

Now Your Turn!

Pick the correct output for the given input

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

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 2 ≤ n ≤ 105
  • 1 ≤ k < n

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int treeCutting(int n, int k, vector<vector<int>>& edges) {
// Write your code here
return 0;
}
};
Test Case

Input:

Edges
K
N