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

3
 / \
2   3
 \   \
  3   1

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

3
   / \
  4   5
 / \   \
1   3   1

Maximum amount of money the thief can rob = 4 + 5 = 9.

LeetCode上的原题,请参见我之前的博客House Robber III

解法一:

class Solution {
public:
int houseRobber3(TreeNode* root) {
unordered_map<TreeNode*, int> m;
return helper(root, m);
}
int helper(TreeNode *root, unordered_map<TreeNode*, int> &m) {
if (!root) return ;
if (m.count(root)) return m[root];
int val = ;
if (root->left) {
val += helper(root->left->left, m) + helper(root->left->right, m);
}
if (root->right) {
val += helper(root->right->left, m) + helper(root->right->right, m);
}
val = max(val + root->val, helper(root->left, m) + helper(root->right, m));
m[root] = val;
return val;
}
};

解法二:

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

[LintCode] House Robber III 打家劫舍之三的更多相关文章

  1. [LeetCode] House Robber III 打家劫舍之三

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  2. [LeetCode] 337. House Robber III 打家劫舍之三

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  3. [LeetCode] 337. House Robber III 打家劫舍 III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  4. [LintCode] House Robber II 打家劫舍之二

    After robbing those houses on that street, the thief has found himself a new place for his thievery ...

  5. 337 House Robber III 打家劫舍 III

    小偷又发现一个新的可行窃的地点. 这个地区只有一个入口,称为“根”. 除了根部之外,每栋房子有且只有一个父房子. 一番侦察之后,聪明的小偷意识到“这个地方的所有房屋形成了一棵二叉树”. 如果两个直接相 ...

  6. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  7. [LeetCode] House Robber II 打家劫舍之二

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  8. Leetcode 337. House Robber III

    337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...

  9. 337. House Robber III(包含I和II)

    198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...

随机推荐

  1. url地址中 "&" "/"等符号的转义处理(转)

    URL出现了有+,空格,/,?,%,#,&,=等特殊符号的时候,可能在服务器端无法获得正确的参数值,如何是好? 解决办法:将这些字符转化成服务器可以识别的字符,对应关系如下: URL中的特殊字 ...

  2. 在Salesforce中避免对Trigger中Update的无限循环操作

    在Salesforce中避免对Trigger中Update的无限循环操作: 处理Trigger的时候会有这么一个场景:在Trigger中想修改该Object的某些字段的值,那么如果们在程序中再用代码的 ...

  3. 汇编学习(四)——算术运算程序

    (一)跳转指令 一.无条件跳转指令(不管标志寄存器,执行到这句直接跳转) 1.段内直接跳转指令 (1)指令格式: JMP SHORT short_label; IP<--IP+DB,即代码直接跳 ...

  4. 删除表数据drop、truncate和delete的用法

    说到删除表数据的关键字,大家记得最多的可能就是delete了 然而我们做数据库开发,读取数据库数据.对另外的两兄弟用得就比较少了 现在来介绍另外两个兄弟,都是删除表数据的,其实也是很容易理解的 老大- ...

  5. Junit的简单使用

    Junit是一个很好用的单元测试工具,下面是使用Junit来测试方法的简单案例: import java.util.ArrayList; import java.util.Iterator; impo ...

  6. hdu1150 匈牙利

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1150 题目大意:有两台机器A和B以及N个需要运行的任务.每台机器有M种不同的模式,而每个任务都恰好在 ...

  7. 仓库如何盘点 打印扫描一体PDA盘点机提升库存盘点效率

    仓库盘点是对仓储货品的收发结存等活动进行有效控制,保证仓储货品完好无损.帐物相符,确保生产正常进行,规范公司物料的盘点作业.盘点需人工操作,费时费力,PDA盘点机的出现大幅提升了盘点效率,减轻了工作人 ...

  8. html5大纲

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. bzoj1008 [HNOI2008]越狱

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5099  Solved: 2207 Description 监狱有 ...

  10. 只有火狐识别的css

    写在css里写只有火狐可以识别,其他浏览器不行. 就算写在@media only screen and (min-width: 960px) and (max-width: 1200px){ 里面也行 ...