I knew House Robber III on a binary tree pretty well, so seeing it extended to n-ary felt manageable at first.
Model the problem as a tree DP where each node returns two values: the maximum sum when the node is robbed and when it is not. Use post-order traversal to combine children's results: if the node is robbed, children cannot be robbed; if not, take the max of each child's two states. Finally, return the max of the root's two states.
Pro tip: Explicitly define the two DP states and show how they transition; this demonstrates you understand the core recurrence and can avoid common pitfalls like double-counting or missing the 'skip' case.
Implement a TreeNode class with a value and a list of children to represent the n-ary tree.
For each node, compute two values: max sum if the node is robbed (rob) and max sum if it is not robbed (skip).
Recursively process all children first, then compute the current node's rob and skip values using children's results.
For rob: node value + sum of children's skip. For skip: sum of max(rob, skip) for each child.
After processing the root, return max(root.rob, root.skip).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.