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.

这道题是之前那两道 House Robber II 和 House Robber 的拓展,这个小偷又偷出新花样了,沿着二叉树开始偷,碉堡了,题目中给的例子看似好像是要每隔一个偷一次,但实际上不一定只隔一个,比如如下这个例子:

       /

     /

   /
  

如果隔一个偷,那么是 4+2=6,其实最优解应为 4+3=7,隔了两个,所以说纯粹是怎么多怎么来,那么这种问题是很典型的递归问题,可以利用回溯法来做,因为当前的计算需要依赖之前的结果,那么对于某一个节点,如果其左子节点存在,通过递归调用函数,算出不包含左子节点返回的值,同理,如果右子节点存在,算出不包含右子节点返回的值,那么此节点的最大值可能有两种情况,一种是该节点值加上不包含左子节点和右子节点的返回值之和,另一种是左右子节点返回值之和不包含当期节点值,取两者的较大值返回即可,但是这种方法无法通过 OJ,超时了,所以必须优化这种方法,这种方法重复计算了很多地方,比如要完成一个节点的计算,就得一直找左右子节点计算,可以把已经算过的节点用 HashMap 保存起来,以后递归调用的时候,现在 HashMap 里找,如果存在直接返回,如果不存在,等计算出来后,保存到 HashMap 中再返回,这样方便以后再调用,参见代码如下:

解法一:

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 ;
if (m.count(root)) return m[root];
int val = ;
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] 就是不包含左子节点值的最大值加上不包含右子节点值的最大值,和当前节点值之和,返回即可,参见代码如下:

解法二:

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

下面这种解法由网友 edyyy 提供,仔细看了一下,也非常的巧妙,思路和解法二有些类似。这里的 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 = , r = ;
return helper(root, l, r);
}
int helper(TreeNode* node, int& l, int& r) {
if (!node) return ;
int ll = , lr = , rl = , rr = ;
l = helper(node->left, ll, lr);
r = helper(node->right, rl, rr);
return max(node->val + ll + lr + rl + rr, l + r);
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/337

类似题目:

House Robber II

House Robber

参考资料:

https://leetcode.com/problems/house-robber-iii/

https://leetcode.com/problems/house-robber-iii/discuss/79333/Simple-C%2B%2B-solution

https://leetcode.com/problems/house-robber-iii/discuss/79363/Easy-understanding-solution-with-dfs

https://leetcode.com/problems/house-robber-iii/discuss/79330/Step-by-step-tackling-of-the-problem

LeetCode All in One 题目讲解汇总(持续更新中...)

最新文章

  1. 注解spring
  2. swift相关
  3. faster r-cnn 在CPU配置下训练自己的数据
  4. mount: unknown filesystem type &#39;ntfs&#39;
  5. iOS 在xib或storyboard里为控件添加圆角、外框和外框颜色
  6. UnicodeDecodeError: &#39;ascii&#39; codec can&#39;t decode byte 0xe5 in position 0: ordinal not in range(128)
  7. PAT (Basic Level) Practise:1037. 在霍格沃茨找零钱
  8. 复杂表格的树形结构的添加删除行div实现
  9. 【Java 进阶篇】【第一课】String类
  10. #include &lt;boost/array.hpp&gt;
  11. WPF4.5新特性(MSDN的翻译读不太懂)
  12. JavaWeb学习之JDBC API中常用的接口和类
  13. c语言变量类型联想
  14. WCF服务寄宿到IIS
  15. cocos2d-x 游戏开发之有限状态机(FSM) (一)
  16. 转)ZooKeeper的实现分析
  17. Java 多线程 sleep方法与wait方法的区别
  18. UVA12627-Erratic Expansion(递归)
  19. 有关Web常用字体的研究?
  20. 微服务日志之.NET Core使用NLog通过Kafka实现日志收集

热门文章

  1. Entity Framework 6 中如何获取 EntityTypeConfiguration 的 Edm 信息?(三)
  2. Django学习笔记(15)——中间件
  3. Logstash处理数据用法示例---待完善
  4. C# NPOI Export DataTable C# NPOI导出DataTable 单元格自适应大小
  5. GIT版本管理工具教程
  6. Dikstra 堆优化板子
  7. SPC软控件提供商NWA的产品在各行业的应用(化工行业)
  8. OC-RunLoop运行循环
  9. AI 的架构与核心
  10. Scrum 冲刺第五篇