Overview
This combines Tree DFS with the House Robber state machine logic. A classic "DP on Trees" problem.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Rob houses arranged in a binary tree where you cannot rob two directly linked houses.
This combines Tree DFS with the House Robber state machine logic. A classic "DP on Trees" problem.
Instead of returning a single integer, the recursive DFS function returns an array of two values: `[maxIfRobbed, maxIfSkipped]`.
If you rob the current node, you CANNOT rob its children. So `robThis = node.val + leftSkipped + rightSkipped`.
If you skip the current node, you have the CHOICE to rob or skip the children (whichever is better). `skipThis = max(left) + max(right)`.