[LeetCode] 337. House Robber III

House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that “all houses in this place forms a binary tree”. It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
Example 1:
这里写图片描述

解析

只要偷的house不是相邻的,即没有父子关系就可以,求最大偷到的值。

解法1:

按正常递归来解,对于某一个节点,如果其左子节点存在,我们通过递归调用函数,算出不包含左子节点返回的值,同理,如果右子节点存在,算出不包含右子节点返回的值,那么此节点的最大值可能有两种情况,一种是该节点值加上不包含左子节点和右子节点的返回值之和,另一种是左右子节点返回值之和不包含当期节点值,取两者的较大值返回即可。
但是上述方法会超时,所以通过建立要给hash表来存储各个节点的最大值,如果存在可以直接返回hash值,如果不存在则计算出来并保存到hash表中。

class Solution {
public:
    int rob(TreeNode* root) {
        unordered_map<TreeNode*, int> m;
        return dfs(root, m);

    }
    int dfs(TreeNode *root, unordered_map<TreeNode*, int> &m){
        if(!root) return 0;
        if(m.count(root)) return m[root];
        int val = 0;
        if(root->left)
            val += dfs(root->left->left, m) + dfs(root->left->right, m);
        if(root->right)
            val += dfs(root->right->left, m) + dfs(root->right->right,m);
        val = max(val + root->val, dfs(root->left, m) + dfs(root->right, m));
        m[root] = val;
        return val;
    } 
};

解法2:

让递归函数返回一个大小为2的一维数组res,其中res[0]表示不包含当前节点值的最大值,res[1]表示包含当前值的最大值。
那么我们在遍历某个节点时,首先对其左右子节点调用递归函数,分别得到包含与不包含左子节点值的最大值,和包含于不包含右子节点值的最大值;
当前节点的res[0]就是左子节点两种情况的较大值加上右子节点两种情况的较大值;
res[1]就是不包含左子节点值的最大值加上不包含右子节点值的最大值,和当前节点值之和,返回即可。

class Solution {
public:
    int rob(TreeNode* root) {
        vector<int> res = dfs(root);
        return max(res[0], res[1]);
    }
    vector<int> dfs(TreeNode *root) {
        if(!root) return vector<int>(2,0);
        vector<int> left = dfs(root->left);
        vector<int> right = dfs(root->right);
        vector<int> res(2, 0);
        res[0] = max(left[0], left[1]) + max(right[0], right[1]);
        res[1] = root->val + left[0] + right[0];
        return res;
    }

};

解法3:

构建一个helper函数返回当前结点为根结点的最大rob的钱数,里面的两个参数l和r表示分别从左子结点和右子结点开始rob,分别能获得的最大钱数。在递归函数里面,如果当前结点不存在,直接返回0。否则我们对左右子结点分别调用递归函数,得到l和r。
另外还得到四个变量,ll和lr表示左子结点的左右子结点的最大rob钱数,rl和rr表示右子结点的最大rob钱数。那么我们最后返回的值其实是两部分的值比较,其中一部分的值是当前的结点值加上ll, lr, rl, 和rr这四个值,这不难理解,因为抢了当前的房屋,那么左右两个子结点就不能再抢了,但是再下一层的四个子结点都是可以抢的;另一部分是不抢当前房屋,而是抢其左右两个子结点,即l+r的值,返回两个部分的值中的较大值即可。

class Solution {
public:
    int rob(TreeNode* root) {
        int l = 0, r = 0;
        return helper(root, l, r);
    }
    int helper(TreeNode *root, int& l, int& r) {
        if (!root) return 0;
        int ll = 0, lr = 0, rl = 0, rr = 0;
        l = helper(root->left, ll, lr);
        r = helper(root->right, rl, rr);
        return max(root->val + ll + lr + rl + rr, l + r);
    }
};

参考

http://www.cnblogs.com/grandyang/p/5275096.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值