Skip to content

Practice Find the Maximum Sum of Node Values

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3068
LeetCode Problem

Find the Maximum Sum of Node Values

Problem Statement

Given a tree on nodes 0 through n - 1 described by edges, node values nums, and a positive integer k, you may choose an edge [u, v] and replace both endpoint values with nums[u] XOR k and nums[v] XOR k. Repeat this operation any number of times, including zero. Return the maximum possible sum of all node values.

Source: Find the Maximum Sum of Node Values on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,2,1], k = 3, edges = [[0,1],[0,2]]
Output
6

# Example 2

Input
nums = [2,3], k = 7, edges = [[0,1]]
Output
9

# Example 3

Input
nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]]
Output
42

Constraints

2 <= n == nums.length <= 2 * 10^4
1 <= k <= 10^9
0 <= nums[i] <= 10^9
edges.length == n - 1
edges[i].length == 2
0 <= edges[i][0], edges[i][1] <= n - 1
The input is generated such that edges represent a valid tree.

Related LeetCode Problems

Practice more problems similar to Find the Maximum Sum of Node Values.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.