House Robber III
Given the root of a binary tree of house values, a thief cannot rob two directly-connected (parent-child) houses. Return the maximum total value the thief can rob without alerting the police.
Open official problem prompt ↗Maximize the sum of chosen node values subject to never choosing a parent and its child together.
Planning which offices to raid in a company org chart: raiding a manager forbids raiding their direct reports, so you weigh each subtree's best inclusive and exclusive plans.
- Input
- root = [3,2,3,null,3,null,1]
- Output
- 7
- Why
- Robbing the root 3 plus the two lower 3s (skipping the middle layer) gives 3+3+1=7, the best legal choice.
The number of nodes is in the range [1, 10^4]0 <= Node.val <= 10^4