```
class Solution {
private:
pair<int, int> cookRob(TreeNode* root) {
if (!root)
return {0, 0};
pair<int, int> left = cookRob(root->left);
pair<int, int> right = cookRob(root->right);
return {max(root->val + left.second + right.second, left.first + right.first), left.first + right.first};
}
public:
int rob(TreeNode* root) {
pair<int, int> ans = cookRob(root);
return ans.first;
}
};
```