题目:入室盗贼3
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 1Maximum amount of money the thief can rob = 3 + 3 + 1 = 7 .
Example 2:
3 / \ 4 5 / \ \ 1 3 1Maximum amount of money the thief can rob = 4 + 5 = 9 .
题意:
小偷又给自己找了一个新的偷盗场所。这片区域只有一个入口,叫做“根”。除了根以外,每一个房间有且仅有一个父级房间。在踩点之后,聪明的盗贼发现“所有的房间形成了一棵二叉树”。如果两个有边直接相连的房间在同一晚上都失窃,就会自动联络警察。
判断盗贼在不惊动警察的情况下最多可以偷到的金钱数目。
转载:http://www.cnblogs.com/grandyang/p/5275096.html
思路一:
题目中给的例子看似好像是要每隔一个偷一次,但实际上不一定只隔一个,比如如下这个例子:
4 / 1 / 2 / 3如果隔一个偷,那么是4+2=6,其实最优解应为4+3=7,隔了两个,所以说纯粹是怎么多怎么来,那么这种问题是很典型的递归问题,我们可以利用回溯法来做,因为当前的计算需要依赖之前的结果,那么我们对于某一个节点,如果其左子节点存在,我们通过递归调用函数,算出不包含左子节点返回的值,同理,如果右子节点存在,算出不包含右子节点返回的值,那么此节点的最大值可能有两种情况,一种是该节点值加上不包含左子节点和右子节点的返回值之和,另一种是左右子节点返回值之和不包含当期节点值,取两者的较大值返回即可,但是这种方法无法通过OJ,超时了。
代码:C++版:Time Limit Exceeded
/** * 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) { if (!root) return 0; int val = 0; if (root->left) { val += rob(root->left->left) + rob(root->left->right); } if (root->right) { val += rob(root->right->left) + rob(root->right->right); } return max(val + root->val, rob(root->left) + rob(root->right)); } };
思路二:
由于上面的方法超时了,所以我们必须优化上面的方法,上面的方法重复计算了很多地方,比如要完成一个节点的计算,就得一直找左右子节点计算,我们可以把已经算过的节点用哈希表保存起来,以后递归调用的时候,现在哈希表里找,如果存在直接返回,如果不存在,等计算出来后,保存到哈希表中再返回,这样方便以后再调用。
代码:C++版:36ms
/** * 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) { 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]; //查找map中是否存在该节点,存在则直接返回值 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的一维数组res,其中res[0]表示不包含当前节点值的最大值,res[1]表示包含当前值的最大值,那么我们在遍历某个节点时,首先对其左右子节点调用递归函数,分别得到包含与不包含左子节点值的最大值,和包含于不包含右子节点值的最大值,那么当前节点的res[0]就是左子节点两种情况的较大值加上右子节点两种情况的较大值,res[1]就是不包含左子节点值的最大值加上不包含右子节点值的最大值,和当前节点值之和,返回即可。
代码:C++版:20ms
/** * 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) { 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] = left[0] + right[0] + root->val; //包含当前节点 return res; } };
思路四:
与以上思路基本相同,只是采用pair的方式存储每次的中间结果,pair.first表示包含该节点的情况下的最大值,pair.second表示不包含该节点时候的最大值。
代码:C++版:16ms
/** * 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) { auto p = search(root); return max(p.first, p.second); } pair<int, int> search(TreeNode *root) { if (!root) return {0, 0}; auto pl = search(root->left); auto pr = search(root->right); return make_pair(root->val + pl.second + pr.second, max(pl.first, pl.second) + max(pr.first, pr.second)); } };