【LeetCode】337. 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:

     3
    / \
   2   3
    \   \ 
     3   1
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

Example 2:

     3
    / \
   4   5
  / \   \ 
 1   3   1
Maximum amount of money the thief can rob = 4 + 5 = 9.


题目大概描述了有一个小偷来到一个地方偷东西。这个地方有很多个地点,相邻的地点相连的话,可以构成一棵二叉树。小偷从这个二叉树的根节点进入,偷取东西。节点上的值为物品价格,但有一个限制条件是:所偷的地点不能够相邻。求能获得的最大价值。

问题可以转换为:

给定一棵二叉树, 选定一些不相邻(不互为父子关系)的节点, 对这些节点求和,求和的最大值.


解题思路:

对于一个节点,它可能有两种状态,偷或不偷。

一个节点是否可偷,取决于它的父节点。若父节点已经被偷,则该节点不能被偷;若父节点没有被偷,则该节点可以选择偷或者不偷。

因此可以用递归的方法:

函数int steal(TreeNode *root, bool canrob); 其中canrob表示root节点是否可以偷。根据canrob的取值,递归调用该节点的左右儿子。

在搜索的过程中,同一节点可能被访问多次,因此可以记录已经计算的节点的值,减少递归次数。


参考代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int rob(TreeNode* root) {
        return steal(root,true);
    }
    int steal(TreeNode* root, bool canrob){
        if (!root)return 0;
        auto p = make_pair(root,canrob);
        if (rec.count(p))return rec[p];
        if (canrob){
            int r = steal(root->left, false) + steal(root->right,false) + root->val;
            int nr = steal(root->left, true) + steal(root->right,true);
            int m = max(r,nr);
            rec[p] = m;
            return m;
        }
        int m = steal(root->left, true) + steal(root->right,true);
        rec[p] = m;
        return m;
    }
private:
    map<pair<TreeNode*, bool>, int > rec;
};


该问题也可以用动态规划的方法解决:

使用数组dp0记录某个节点没有被偷的最大价值,数组dp1记录某个节点被偷的最大价值

状态转移方程为:

        dp0[root] = max(max(dp0[root->left]+dp0[root->right],dp1[root->left]+dp1[root->right]), max(dp1[root->left]+dp0[root->right],dp0[root->left]+dp1[root->right]));
        dp1[root] = dp0[root->left] + dp0[root->right] + root->val;

参考代码:

class Solution {
public:
    int rob(TreeNode* root) {
        LRV(root);
        return max(dp0[root], dp1[root]);
    }
    void LRV(TreeNode * root){
        if (!root)return;
        LRV(root->left);
        LRV(root->right);
        dp0[root] = max(max(dp0[root->left]+dp0[root->right],dp1[root->left]+dp1[root->right]), max(dp1[root->left]+dp0[root->right],dp0[root->left]+dp1[root->right]));
        dp1[root] = dp0[root->left] + dp0[root->right] + root->val;
    }
private:
    map<TreeNode *, int> dp0; // not rob
    map<TreeNode *, int> dp1; //rob
};



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值