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.

这题一开始用递归 结果超时;随机加cache变成DP。。通过了

helper1表示抢root

helper2表示不抢root;此处注意不抢root时 即可以抢root.left和root.right 也可以不抢它们 二取其一 所以注意42-47行

 
 /**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: The maximum amount of money you can rob tonight
*/
Map<TreeNode, Integer> map1 = new HashMap<TreeNode, Integer>();
Map<TreeNode, Integer> map2 = new HashMap<TreeNode, Integer>();
public int houseRobber3(TreeNode root) {
// write your code here
if(root==null) return 0;
return Math.max(helper1(root), helper2(root));
}
// include root
private int helper1(TreeNode root){
if(map1.containsKey(root)){
return map1.get(root);
}
int sum = root.val;
if(root.left!=null){
sum += helper2(root.left);
}
if(root.right!=null){
sum += helper2(root.right);
}
map1.put(root, sum);
return sum;
}
// not include root
private int helper2(TreeNode root){
if(map2.containsKey(root)){
return map2.get(root);
}
int sum = 0;
if(root.left!=null){
sum += Math.max(helper1(root.left), helper2(root.left));
}
if(root.right!=null){
sum += Math.max(helper1(root.right), helper2(root.right));
}
map2.put(root, sum);
return sum;
}
}

House Robber III的更多相关文章

  1. [LintCode] 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

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

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

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

  4. 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:二叉树下的不能相邻,求能 ...

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

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

  6. LeetCode House Robber III

    原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: The thief has found himself a new place ...

  7. Java [Leetcode 337]House Robber III

    题目描述: The thief has found himself a new place for his thievery again. There is only one entrance to ...

  8. [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 ...

  9. [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 ...

  10. 【LeetCode】House Robber III(337)

    1. Description The thief has found himself a new place for his thievery again. There is only one ent ...

随机推荐

  1. ThinkPHP执行原生的SQL语句

    执行原生的SQL语句: $sql="insert select update delete...."; ①查询语句:   $model对象 -> query($sql);  ...

  2. 雷林鹏分享:jQuery EasyUI 窗口 - 自定义窗口工具栏

    jQuery EasyUI 窗口 - 自定义窗口工具栏 默认情况下,窗口(window)有四个工具:collapsible.minimizable.maximizable 和 closable.比如我 ...

  3. Bash Shell 注释多行的几种方法(转)

    很实用的小技巧. 我们shell脚本写好了,但是想一行一行测试,怎么办. 笨方法:每行前面加一个 #,有时候我们原脚本里面本来就有注释,所以想再恢复的时候就麻烦了. Bash Shell 注释多行的几 ...

  4. 如何启动iis(Internet 信息服务(IIS)管理器)

    Internet 信息服务(IIS)管理器 启动 IIS 管理器1.从“开始”菜单,指向“管理工具”,然后单击“Internet 信息服务 (IIS) 管理器”. 从“运行”对话框启动 IIS 管理器 ...

  5. Hadoop/HBase Capacity Planning

    http://blog.cloudera.com/blog/2010/08/hadoophbase-capacity-planning/

  6. apiClould微信三方登录,qq三方登录

    三方登录需要在config.xml文件中配置一下,res文件下需要新建一个key.xml文件中也需要配置,请点击链接查看详情 微信:https://docs.apicloud.com/Client-A ...

  7. Java编码 蛇形矩阵的构建与遍历输出

    一.蛇形矩阵的构建,并按行输出 例: 输入:n, 生成n*n的蛇形矩阵 1 2 3 8 9 4 7 6 5 输出:1 2 3 8 9 4 7 6 5 java编码 public static void ...

  8. python记录_day03 字符串

    python基本数据类型回顾 1. int 整数 2. str 字符串. 不会用字符串保存大量的数据 3. bool 布尔值. True, False 4. list 列表(重点) 存放大量的数据 5 ...

  9. 6月17 ThinkPHP连接数据库------数据的修改及删除

    1.数据修改操作 save()  实现数据修改,返回受影响的记录条数 具体有两种方式实现数据修改,与添加类似(数组.AR方式) 1.数组方式 a)         $goods = D(“Goods” ...

  10. javaweb项目静态资源被拦截的解决方法

    <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>/*< ...