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. PC端、移动端的页面适配及兼容处理

    转自 一.关于移动端兼容性 目前针对跨终端的方案,主要分为两大阵营:一套资源Vs两套资源. 第一种是通过响应式或页面终端判断去实现一套资源适配所有终端: 第二种是通过终端判断分别调取两套资源以适配所有 ...

  2. samtools faidx输出的fai文件格式解析 | fasta转bed | fasta to bed

    fai示例: Sc0000003 2774837 10024730 60 61 Sc0000004 2768176 12845826 60 61 Sc0000005 2756750 15660150 ...

  3. file 文件处理

    python文件处理 #写文件 >>> f=open("test.log","w") #"w"写入模式 >>& ...

  4. CentOS7 安装redis4:

      phpredis-4.0.12.tar.gz:下载:wget http://download.redis.io/releases/redis-4.0.12.tar.gz   $ tar -zxvf ...

  5. loj#6062. 「2017 山东一轮集训 Day2」Pair hall定理+线段树

    题意:给出一个长度为 n的数列 a和一个长度为 m 的数列 b,求 a有多少个长度为 m的连续子数列能与 b匹配.两个数列可以匹配,当且仅当存在一种方案,使两个数列中的数可以两两配对,两个数可以配对当 ...

  6. 创建springboot的聚合工程(三)

    springboot聚合工程之添加mybatis数据库持久化操作 在boot-polymer-repository工程添加mybatis的相关依赖 <project xmlns="ht ...

  7. SQL 经典回顾:JOIN 表连接操作不完全指南

    ​   2017-02-23 小峰 ITPUB 点击上方“蓝字”可以关注我们哦  |转载自:码农网 |原文链接:www.codeceo.com/article/sql-join-guide.html ...

  8. python中几种循环体

    条件判断与循环 1.条件判断 if <条件判断1>: <执行1> elif <条件判断2>: <执行2> elif <条件判断3>: < ...

  9. 用javascript切换bootstrap的tab

    html: <button class="tabContainer" data-toggle="tab" href="#note" i ...

  10. ScheduledThreadPoolExecutor

    java提供了方便的定时器功能,代码示例: public class ScheduledThreadPool_Test { static class Command implements Runnab ...