LeetCode Problem

How to Solve Maximum Good Subtree Score

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.

GhostInterview Help

Need help with Maximum Good Subtree Score without spending extra time grinding it?

GhostInterview can read Maximum Good Subtree Score from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #3575Binary-tree traversal and state trackingReviewed 2026-03-08
Difficulty
Hard
Primary pattern
Binary-tree traversal and state tracking
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

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

MetricValue
TimeDepends on the final approach
SpaceDepends 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.

How GhostInterview Helps

  • Automatically generates bitmask and DP structure for subtree combination.
  • Highlights the exact DFS traversal order to avoid repeated computation.
  • Identifies invalid digit overlaps and suggests correct merging of subtrees.

Topic Pages

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.

GhostInterview Solver

Need direct help with Maximum Good Subtree Score instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Maximum Good Subtree Score from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.