Practice Maximum Good Subtree Score
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Maximum Good Subtree Score
Problem Statement
You are given an undirected tree rooted at node 0 with n nodes numbered from 0 to n - 1. Each node i has an integer value vals[i], and its parent is given by par[i]. A subset of nodes within the subtree of a node is called good if every digit from 0 to 9 appears at most once in the decimal representation of the values of the selected nodes. The score of a good subset is the sum of the values of its nodes. Define an array maxScore of length n, where maxScore[u] represents the maximum possible sum of values of a good subset of nodes that belong to the subtree rooted at node u, including u itself and all its descendants. Return the sum of all values in maxScore. Since the answer may be large, return it modulo 10^9 + 7.
Source: Maximum Good Subtree Score on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
vals = [2,3], par = [-1,0]
8
# Example 2
vals = [1,5,2], par = [-1,0,0]
15
# Example 3
vals = [34,1,2], par = [-1,0,1]
42
# Example 4
vals = [3,22,5], par = [-1,0,1]
18
Constraints
Related LeetCode Problems
Practice more problems similar to Maximum Good Subtree Score.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.