LeetCode Problem Workspace

Maximum Good Subtree Score

Find the maximum sum of values in a tree subtree without repeating any digit across selected nodes using DFS and bitmasking.

category

6

Topics

code_blocks

0

Code langs

hub

3

Related

Practice Focus

Hard · Binary-tree traversal and state tracking

bolt

Answer-first summary

Find the maximum sum of values in a tree subtree without repeating any digit across selected nodes using DFS and bitmasking.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Binary-tree traversal and state tracking

Try AiBox Copilotarrow_forward

Start by representing each node's value as a bitmask of its digits. Use depth-first search to traverse the tree and combine valid subsets while ensuring no digit repeats. Dynamic programming stores maximum sums for each mask to avoid redundant computation, making the solution efficient for trees with up to 500 nodes.

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 a parent par[i], where par[0] is -1 for the root. Your task is to identify subsets of nodes in the tree such that no digit from 0 to 9 appears more than once across the selected nodes.

A subset of nodes within a subtree is called good if its nodes' decimal digits are all unique. The score of a good subset is the sum of the values of its nodes. Compute the maximum score among all possible good subsets in the tree.

Examples

Example 1

Input: vals = [2,3], par = [-1,0]

Output: 8

Example 2

Input: vals = [1,5,2], par = [-1,0,0]

Output: 15

Example 3

Input: vals = [34,1,2], par = [-1,0,1]

Output: 42

Constraints

  • 1 <= n == vals.length <= 500
  • 1 <= vals[i] <= 109
  • par.length == n
  • par[0] == -1
  • 0 <= par[i] < n for i in [1, n - 1]
  • The input is generated such that the parent array par represents a valid tree.

Solution Approach

Bitmask Representation of Digits

Convert each node's value into a 10-bit mask where each bit represents a digit 0-9. This allows quick checks for overlapping digits when combining nodes.

DFS with Subtree Combination

Perform depth-first search from the root. For each node, recursively compute the best good subset sums of its children and merge them only if their bitmasks do not conflict. Update the maximum score at each step.

Dynamic Programming for Masks

Maintain a DP dictionary mapping bitmasks to their maximum achievable sums. When visiting a node, iterate over existing masks and combine with the current node's mask if there is no overlap, ensuring efficient state tracking across the tree.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(n * 2^10) due to visiting each node and merging at most 1024 masks. Space complexity is O(2^10) for storing DP masks at each node, which is manageable for n <= 500.

What Interviewers Usually Probe

  • The candidate considers digit-level conflicts using bitmasking.
  • DFS traversal with subtree state combination is being implemented.
  • Dynamic programming is used to merge masks and track maximum sums.

Common Pitfalls or Variants

Common pitfalls

  • Not handling digit overlaps correctly when combining subtrees.
  • Overlooking the need to update maximum score for each mask.
  • Attempting brute-force enumeration of all subsets, leading to exponential time.

Follow-up variants

  • Compute maximum good subtree score for binary trees only.
  • Find minimum good subtree score instead of maximum.
  • Restrict nodes to those with values having at most two digits.

FAQ

What is the Maximum Good Subtree Score problem about?

It asks for the largest sum of node values in a tree subset where no digit repeats across the selected nodes.

Which tree traversal is best for this problem?

Depth-first search is preferred as it naturally supports subtree combination and state propagation.

How do bitmasks help in this problem?

Each node's value is converted into a bitmask to quickly check for digit conflicts when merging subsets.

Can this approach handle n = 500 nodes?

Yes, using DP over 2^10 masks ensures efficient computation within reasonable time and space limits.

Is Dynamic Programming required for Maximum Good Subtree Score?

While DFS is essential, DP over digit masks prevents redundant calculations and efficiently tracks maximum sums.

terminal

Solution

Solution 1

#### Python3

1
Maximum Good Subtree Score Solution: Binary-tree traversal and state track… | LeetCode #3575 Hard