There exists an undirected tree with n nodes numbered 0 to n - 1. You are given a 2D integer array edges of length n - 1, where edges[i] = [ui, vi, wi] indicates that there is an edge between nodes ui and vi with weight wi in the tree.
Your task is to remove zero or more edges such that:
k other nodes, where k is given.Return the maximum possible sum of weights for the remaining edges after making the necessary removals.
Example 1:
Input: edges = [[0,1,4],[0,2,2],[2,3,12],[2,4,6]], k = 2
Output: 22
Explanation:

[0, 2, 2], ensuring that no node has edges with more than k = 2 nodes.Example 2:
Input: edges = [[0,1,5],[1,2,10],[0,3,15],[3,4,20],[3,5,5],[0,6,10]], k = 3
Output: 65
Explanation:
k = 3 nodes, we don't remove any edges.Constraints:
2 <= n <= 1051 <= k <= n - 1edges.length == n - 1edges[i].length == 30 <= edges[i][0] <= n - 10 <= edges[i][1] <= n - 11 <= edges[i][2] <= 106edges form a valid tree.Problem #3367 Maximize Sum of Weights after Edge Removals typically involves analyzing a weighted graph or tree and deciding which edges to remove while maximizing the total remaining weight. Since removing an edge changes the structure of the graph, the key idea is to evaluate how each removal affects the overall contribution of the remaining edges.
A common strategy is to treat the structure as a tree and apply DFS-based dynamic programming. During traversal, compute the best contribution each subtree can provide and track the gain or loss when choosing whether to keep or remove an edge. Using a greedy selection mechanism (often with sorting or a priority_queue) helps pick the most beneficial edges to retain while discarding less valuable ones.
This approach processes each node once and aggregates optimal choices from children. Efficient bookkeeping of candidate gains allows the algorithm to scale well. The typical implementation runs in O(n log n) time due to sorting or heap operations, with O(n) auxiliary space for recursion and DP storage.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| DFS + Tree Dynamic Programming with Greedy Edge Selection | O(n log n) | O(n) |
| Graph Traversal with Priority Queue for Edge Gains | O(n log n) | O(n) |
NeetCode
Use these hints if you're stuck. Try solving on your own first.
Can we use DFS based approach here?
For each edge, find two sums: one including the edge and one excluding it.
Watch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
Problems involving tree DP, greedy edge selection, and graph optimization are common in FAANG-style interviews. While this exact problem may vary, the underlying techniques frequently appear in advanced coding interviews.
Priority queues and adjacency lists are commonly used. The adjacency list efficiently represents the graph or tree, while a priority queue helps select edges with the highest contribution when deciding which edges to keep.
Yes, it often involves dynamic programming on trees. Each node aggregates results from its children and determines the best combination of edges to maximize the remaining weight after removals.
The optimal approach generally combines DFS-based tree dynamic programming with greedy selection. Each subtree computes the gain of keeping certain edges, and the algorithm selects the best contributions using sorting or a priority queue.