其他分享
首页 > 其他分享> > 【leetcode】337. 打家劫舍 III

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

 

标签:pre,right,struct,root,337,st,III,leetcode,left
来源: https://www.cnblogs.com/ganxiang/p/14320411.html