【leetcode】337. 打家劫舍 III

struct st  {
    int pre;
    int prepre;
};
#define max(a,b) ((a)>(b))?(a):(b)
struct st recursion(struct TreeNode* root){
    if(!root){
        return (struct st){0,0};
    }
    struct st left = recursion(root->left);
    struct st right = recursion(root->right);
    int tmp=left.pre;
    left.pre=max(left.pre+right.pre, left.prepre+right.prepre+root->val);
    left.prepre=tmp+right.pre;
    return left;    
}
int rob(struct TreeNode* root){
    return recursion(root).pre;
}
原文地址:https://www.cnblogs.com/ganxiang/p/14320411.html