【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;
}

 

posted @ 2021-01-24 11:51  温暖了寂寞  阅读(53)  评论(0编辑  收藏  举报