面试经典 150 题 2.2 —(一维动态规划)— 337. 打家劫舍 III

337. 打家劫舍 III

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> robInternal(TreeNode* root){
        if(root == nullptr){
            return vector<int>{0, 0};
        }
        vector<int> left = robInternal(root->left);
        vector<int> right = robInternal(root->right);

        // 不偷root,那么可以偷、也可以不偷左右节点,此时取最大的情况
        int val0 = max(left[0], left[1]) + max(right[0], right[1]);
        // 偷root,就不能偷左右节点
        int val1 = root->val + left[0] + right[0];
        
        return {val0, val1};
    }
    int rob(TreeNode* root) {
        vector<int> result = robInternal(root);
        return max(result[0], result[1]);
    }
};
// 超出内存限制
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int robInternal(TreeNode* root, unordered_map<TreeNode*, int> map_TreeNode_money){
        if(root == nullptr){
            return 0;
        }
        if(map_TreeNode_money.count(root)){
            return map_TreeNode_money.at(root);
        }
        int money_root_grandsons = root->val;
        if(root->left != nullptr){
            money_root_grandsons += (robInternal(root->left->left, map_TreeNode_money) + robInternal(root->left->right, map_TreeNode_money));
        }
        if(root->right != nullptr){
            money_root_grandsons += (robInternal(root->right->left, map_TreeNode_money) + robInternal(root->right->right, map_TreeNode_money));
        }

        int money_root_sons = robInternal(root->left, map_TreeNode_money) + robInternal(root->right, map_TreeNode_money);
        int result = money_root_sons > money_root_grandsons ? money_root_sons : money_root_grandsons;
        map_TreeNode_money.insert(make_pair(root, result));
        return result;
    }
    int rob(TreeNode* root) {
        unordered_map<TreeNode*, int> map_TreeNode_money;
        return robInternal(root, map_TreeNode_money);
    }
};
// 超出时间限制
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int rob(TreeNode* root) {
        if(root == nullptr){
            return 0;
        }
        int money_root_grandsons = root->val;
        if(root->left != nullptr){
            money_root_grandsons += (rob(root->left->left) + rob(root->left->right));
        }
        if(root->right != nullptr){
            money_root_grandsons += (rob(root->right->left) + rob(root->right->right));
        }

        int money_root_sons = rob(root->left) + rob(root->right);
        return money_root_sons > money_root_grandsons ? money_root_sons : money_root_grandsons;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值