题目描述:

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.

解题思路:

像House Robber I一样,使用动态规划法,对于每个节点,使用两个变量,res[0], res[1],分别表示不选择当前节点子树的数值和,选择当前节点子树的数值和,动态规划的思想,然后递归。

代码如下:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int rob(TreeNode root) {
int[] res = robSub(root);
return Math.max(res[0], res[1]);
} public int[] robSub(TreeNode root){
if(root == null)
return new int[2]; int[] left = robSub(root.left);
int[] right = robSub(root.right); int[] res = new int[2];
res[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]); // do not choose current node
res[1] = root.val + left[0] + right[0]; // choose current node return res;
}
}

  

Java [Leetcode 337]House Robber III的更多相关文章

  1. Leetcode 337. House Robber III

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

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

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

  4. LeetCode 337. House Robber III 动态演示

    每个节点是个房间,数值代表钱.小偷偷里面的钱,不能偷连续的房间,至少要隔一个.问最多能偷多少钱 TreeNode* cur mp[{cur, true}]表示以cur为根的树,最多能偷的钱 mp[{c ...

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

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

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

  7. <LeetCode OJ> 337. House Robber III

    Total Accepted: 1341 Total Submissions: 3744 Difficulty: Medium The thief has found himself a new pl ...

  8. 【LeetCode】337. House Robber III 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【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. 在一个字符串(1<=字符串长度<=10000,全部由大小写字母组成)中找到第一个只出现一次的字符,并返回它的位置

    // test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  2. ios开发小技巧之摇一摇截屏

    1. 监控摇一摇动作 1> 让当前视图控制器成为第一响应者 // 必须先让当前视图控制器成为第一响应者才能响应动作时间 [self becomeFirstResponder]; 2> 实现 ...

  3. [设计模式] 16 迭代器模式 Iterator Pattern

    在GOF的<设计模式:可复用面向对象软件的基础>一书中对迭代器模式是这样说的:提供一种方法顺序访问一个聚合对象中各个元素,而又不需要暴露该对象的内部表示. 类图和实例: 迭代器模式由以下角 ...

  4. win8 ubuntu

    点进去看到几点注意: 1. 如果Windows是UEFI方式安装的,那Ubuntu必须也用UEFI方式安装 2. 必须用64位的Ubuntu安装文件,32位的不能探测EFI 3. 必须用UEFI的方式 ...

  5. 【redis】04set类型和zset类型

    sets类型   sets类型及操作   Set类型是一个集合,他是string类型的无序集合,也就是说咱们的set是没有顺序的, Set是通过hash table实现的,添加.删除和查找的复杂度都是 ...

  6. ZOJ3765 Lights Splay树

    非常裸的一棵Splay树,需要询问的是区间gcd,但是区间上每个数分成了两种状态,做的时候分别存在val[2]的数组里就好.区间gcd的时候基本上不支持区间的操作了吧..不然你一个区间里加一个数gcd ...

  7. hihocoder 1084 扩展KMP && 2014 北京邀请赛 Justice String

    hihocoder 1084 : http://hihocoder.com/problemset/problem/1084 北京邀请赛 Just  String http://www.bnuoj.co ...

  8. [SharePoint 2013 入门教程 2 ] 创建WEB应用程序,网站集,网站

    SharePoint 2013 的 Hello World 由大到小  创建WEB应用程序(老母),网站集(儿子),网站(孙子) 直接确定,其余都默认 填入标题,选好模板.网站集 儿子就有了. 点击页 ...

  9. PHP魔术方法小结.md

    说明 魔术方法就是在特定场景下不需要调用而自动执行的方法.因为有魔术方法,所以我们的类可以写得很灵活~ __construct #构造方法,在类被实例化时自动调用,一般用于初始化操作; __destr ...

  10. 使用phantomjs对页面进行截图

    本文章参考了使用phantomjs操作DOM并对页面进行截图需要注意的几个问题 及 phantomjs使用说明 这两篇文章,初次接触phantomjs的童鞋可以去看下这两篇原文 在学习中可以看下 ph ...