Choose Edges to Maximize Score in a Tree - Problem

Choose Edges to Maximize Score in a Tree

Imagine you're a network engineer tasked with selecting optimal connections in a tree network to maximize bandwidth utilization. You have a weighted tree with n nodes numbered from 0 to n-1, rooted at node 0.

The tree structure is given as a 2D array edges where edges[i] = [parent_i, weight_i] indicates that parent_i is the parent of node i, and the edge between them has weight weight_i. The root node has edges[0] = [-1, -1] since it has no parent.

Your challenge: Select a subset of edges such that no two selected edges share a common node (are adjacent), and the sum of their weights is maximized.

Goal: Return the maximum possible sum of edge weights you can achieve.

Key constraint: Two edges are considered adjacent if they share any common node. For example, edges connecting (A↔B) and (B↔C) are adjacent because they both connect to node B.

Input & Output

example_1.py — Basic Tree
$ Input: edges = [[-1,-1],[0,5],[0,3],[1,4]]
Output: 7
💡 Note: Tree structure: 0->1->3, 0->2. We can select edges (0,2) with weight 3 and (1,3) with weight 4. These edges don't share nodes, giving sum = 3 + 4 = 7.
example_2.py — Single Path
$ Input: edges = [[-1,-1],[0,2],[1,3],[2,1]]
Output: 3
💡 Note: Linear tree: 0->1->2->3. We can only select one edge due to adjacency constraints. Choose edge (1,2) with weight 3 for maximum sum.
example_3.py — Single Node
$ Input: edges = [[-1,-1]]
Output: 0
💡 Note: Tree with only root node has no edges to select, so maximum sum is 0.

Constraints

  • 1 ≤ n ≤ 105
  • edges.length == n
  • edges[i].length == 2
  • 0 ≤ parenti ≤ n - 1
  • -106 ≤ weighti ≤ 106
  • edges[0] = [-1, -1]
  • The input represents a valid tree

Visualization

Tap to expand
Choose Edges to Maximize Score in a Tree INPUT Tree Structure (rooted at 0) 0 1 2 3 5 3 4 edges array: [[-1,-1], [0,5], [0,3], [1,4]] [parent, weight] for each node ALGORITHM (DP) 1 Define DP States dp[v][0]: max without parent edge dp[v][1]: max with parent edge 2 Post-order Traversal Process children before parent Visit: 3 --> 1 --> 2 --> 0 3 Transition Logic If pick edge to child c: - c cannot use its parent edge 4 Calculate Values At each node, choose best DP Computation Node 3: dp[3][0]=0, dp[3][1]=0 Node 2: dp[2][0]=0, dp[2][1]=0 Node 1: dp[1][0]=4, dp[1][1]=0 (pick edge 1-3, weight=4) Node 0: max(4+3, 0+5)=7 Best: pick 1-3 and 0-2 FINAL RESULT Selected Edges (green) 0 1 2 3 5 3 4 Maximum Score 7 = 3 + 4 (edges 0-2, 1-3) Key Insight: The DP approach tracks two states per node: whether we use the edge to parent or not. Adjacent edges share a node, so if we pick edge (u,v), we cannot pick any other edge incident to u or v. This creates optimal substructure: each subtree's solution is independent once we fix the parent edge choice. TutorialsPoint - Choose Edges to Maximize Score in a Tree | Dynamic Programming Approach
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
31.7K Views
Medium Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen