https://leetcode.com/problems/house-robber-iii/description/
树形dp把, dp[i] = max( dp[i->left] + dp[i->right], dp[i] + sum(dp[k]) k为i的下下层 )
class Solution {
public:
int rob(TreeNode* root) {
if (!root) return 0;
int l=0, r= 0;
return tryRob(root, l, r);
}
int tryRob(TreeNode *root, int &lrslt, int &rrslt) {
if (!root) return 0;
int lans = 0, rans = 0, lcans = 0, rcans = 0;
lrslt = tryRob(root->left, lcans, rcans);
rrslt = tryRob(root->right, lans, rans);
return max( root->val + lcans + rcans + lans + rans, lrslt + rrslt );
}
};