dfs学习之打家劫舍(3)

来源:互联网 发布:淘宝收购饿了么 编辑:程序博客网 时间:2024/06/04 00:45

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.
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

分析

这道题属于树形dp问题,即根据二叉树的结构来进行动态规划。虽然整棵树会很大,然后树的形状各式各样,但是如果我抓住了核心部分就可以解决了。核心部分是每个节点的取值如何最大。按照题目的意思,父节点和子节点不能同时取。根据这个意思,就是看取不取这个节点值使得从这个点作为树的根节点的树取值最大。有两种选择,一种就是不取这个点,然后看左右子节点和他们各自子节点的最大值。另外一种就是取了这个点,然后不取左右子节点,再加上他们各自的子节点的分树的最大值,得到这颗树的最大值。对于我的代码的解释,假如该节点为空,则返回两个0的vector。然后再寻找左右子节点,得到了左右两个vector。定义一个vector,第一个值就是不取该点值的做法,第二个值就是取了这个值的做法。最终在主函数里面,判断这两个值的大小情况,取其较大值。

代码

using namespace std;class Solution {public:    int rob(TreeNode* root) {        vector<int> nums = robHouse(root);        return nums[0]>nums[1] ? nums[0] : nums[1];    }    vector<int> robHouse(TreeNode* node) {        if (node == NULL) return vector<int>(2, 0);        vector<int> left = robHouse(node->left);        vector<int> right = robHouse(node->right);        vector<int> result(2, 0);        int temp1 = left[0] > left[1] ? left[0] ? left[1];        int temp2 = right[0] > right[1] ? right[0] ? right[1];        result[0] = temp1 + temp2;        result[1] = left[0] + right[0] + node->val;        return result;    }};

附加

其他情况

在看不懂题目的时候,我有两个理解,一是分层选择,每层都取完所有,最终分别将奇偶层的全部相加,看奇偶层哪个最大得到结果。二还是分层选择,但不是简单的奇偶层全部相加,而是动态规划,按照每层都会取完,然后判断取不取这层达到最优解,和题目就差一个是否全部取完这个点。下面添加我的错误理解的代码

错误思路1

using namespace std;class Solution {public:    int rob(TreeNode* root) {        vector<int> result = robHouse(root, 1);        return result[0]>result[1] ? result[0]:result[1];    }    vector<int> robHouse(TreeNode* root, int level) {        vector<int> result(2,0);        vector<int> Left(2,0);        vector<int> Right(2,0);        int odd = 0, even = 0;        if (root == NULL) return result;        if (level % 2 == 0) even += root->val;        else odd += root->val;        if (root->left) Left = robHouse(root->left, level+1);        if (root->right) Right = robHouse(root->right, level+1);        result[0] = odd + Left[0] + Right[0];        result[1] = even + Left[1] + Right[1];        return result;    }};

错误思路2

using namespace std;class Solution {public:    int rob(TreeNode* root) {        vector<int> nums;        findLevel(root, 1, nums);        if (root == NULL) return 0;        int _size = nums.size();        int* f = new int[_size];        for (int i = 0; i < _size; i++) {            f[i] = 0;        }        for (int i = 0; i < _size; i++) {            if (i == 0) {                f[i] = nums[i];                continue;            }            if (i == 1) {                f[i] = nums[1]>nums[0] ? nums[1] : nums[0];                continue;            }            int choosing = f[i-1];            int choosed = nums[i]+f[i-2];            f[i] = choosed>choosing ? choosed : choosing;        }        int result = f[_size-1];        delete []f;        return result;    }    void findLevel(TreeNode* root, int level, vector<int>& levels) {        if (root == NULL) return;        while (levels.size() < level) levels.push_back(0);        levels[level-1] += root->val;        if (root->left) findLevel(root->left, level+1, levels);        if (root->right) findLevel(root->right, level+1, levels);        return;    }};

题目地址:LeetCode
参考的思路:Grandyang的博客

原创粉丝点击