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. English trip M1 - AC11 May I Help You? 我能帮到你吗? Teacher:Lamb

    In this lesson you will learn to ask for things in shops  在本课程中,您将学习如何在商店中寻找东西 课上内容(Lesson) How are ...

  2. PHP操作MySQL数据库--PHP的应用

    一.Apache服务器的安装 <1>安装版(计算机相关专业所用软件---百度云链接下载)-直接install<2>非安装版(https://www.apachehaus.com ...

  3. Django初始化之基本操作

    1.指定要安装的Django版本 C:\Users\win7>pip install Django==1.11.8 2.查看安装的django版本 C:\Users\win7>pip sh ...

  4. python3-知识扩展扫盲易忘-zip的用法

    >>>a = [1,2,3] >>> b = [4,5,6]>>> c = [4,5,6,7,8]>>> zipped = zi ...

  5. Vue.js的后端数据支持:使用Express建立app, 并使用MongoDB数据库。

    需要用到的backed tech stack: Node: JavaScript on the server/backend. That's basically what it is, but mor ...

  6. salt相关

      salt安装  https://docs.saltstack.com/en/latest/topics/installation/index.html#quick-install   salt远程 ...

  7. 【洛谷p1012】拼数

    (今天yuezhuren大课间放我们出来了……) (另外今天回了两趟初中部) 拼数[传送门] 洛谷算法标签: (然鹅这两个学的都不好,能过真的how strange) 开始的时候没读题啊,直接暴力so ...

  8. Maximum Questions CodeForces - 900E (字符串,dp)

    大意:给定长$n$的字符串$s$, 只含'a','b','?', '?'可以替换为任意字符, 在给定长$t$的字符串, "ababab...", 求替换尽量少的'?', 使得$s$ ...

  9. 20165309 实验一 Java开发环境的熟悉

    20165309 实验一 Java开发环境的熟悉 一.实验内容及步骤 (一)命令行下Java程序开发 在Linux下用ctrl+alt+T打开终端,用mkdir创建文件夹后cd进入. 在vim下键入如 ...

  10. IntelliJ Idea设置单击打开文件或者双击打开文件、自动定位文件所在的位置