题目描述:

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. Linux开机执行bash脚本

    问题描述:     Linux开机执行bash脚本     问题解决:         (1)在 /etc/init.d文件夹中新建一个脚本myinit                     (2) ...

  2. 基于密度的聚类之Dbscan算法

    一.算法概述 DBSCAN(Density-Based Spatial Clustering of Applications with Noise)是一个比较有代表性的基于密度的聚类算法.与划分和层次 ...

  3. JavaScript高级---门面模式设计

    门面模式 两个作用: 1.简化类的接口 2.消除类与使用它的客户代码之间的耦合 门面模式常常是开发人员最亲密的朋友.它几乎是所有javascript库的核心原则 门面模式的目的是为了让开发人员用更简单 ...

  4. 【Asp.Net MVC--资料汇总】杂七杂八

    Html.RenderPartial与Html.RenderAction的区别 http://blog.sina.com.cn/s/blog_8278b1800100zkn0.html ASP.NET ...

  5. POJ 1775

    #include <iostream> using namespace std; ,,,,,,,,,}; bool boo; void DFS(int time,int sum); int ...

  6. Android 虚拟机安装SD卡

    在cmd命令行下,进入platform-tools目录下.   1.创建sdcard   mksdcard -l mycard 256M E:\android\myCards\mysdcard.img ...

  7. CKeditor 配置使用

    CKeditor 配置使用 一.使用方法:1.在页面<head>中引入ckeditor核心文件ckeditor.js<script type="text/javascrip ...

  8. 如何在Mininet中修改host的IP地址

    how to update virtual host's IP in mininet? I got it! do like this: mininet> py h1.setIP('10.0.0. ...

  9. 【memcache缓存专题(1)】memcache的介绍与应用场景

    简介 Memcached是一个高性能的分布式的内存对象缓存系统,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各 ...

  10. JDK安装配置问题

    JDK安装过程中会有两个安装提示,一个是jdk的安装,一个是jre的安装