原题链接在这里:https://leetcode.com/problems/house-robber-iii/

题目:

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.

题解:

List some examples and find out this has to be done with DFS.

One or null node is easy to think, thus use DFS bottom-up, devide and conquer.

Then it must return value on dfs. Each dfs needs current node and return [robRoot, notRobRoot], which denotes rob or skip current node.

robRoot = notRobLeft + notRobRight + root.val

notRobRoot = Math.max(robLeft, notRobLeft) + Math.max(robRight, notRobRight).

Time Complexity: O(n).

Space: O(logn). 用了logn层stack.

AC Java:

 /**
* 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 = dfs(root);
return Math.max(res[0], res[1]);
}
private int [] dfs(TreeNode root){
int [] dp = new int[2];
if(root == null){
return dp;
}
int [] left = dfs(root.left);
int [] right = dfs(root.right);
//dp[0]表示偷root的, 那么左右都不能偷, 所以用left[1], right[1].
dp[0] = left[1] + right[1] + root.val;
//dp[1]表示不偷root的, 那么左右偷不偷都可以, 取最大值
dp[1] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
return dp;
}
}

类似House RobberHouse Robber II.

LeetCode House Robber III的更多相关文章

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

  4. [LeetCode] House Robber II 打家劫舍之二

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  5. [LeetCode] House Robber 打家劫舍

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  6. [LintCode] House Robber III 打家劫舍之三

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

  7. LeetCode House Robber

    原题链接在这里:https://leetcode.com/problems/house-robber/ 题目: You are a professional robber planning to ro ...

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

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

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

随机推荐

  1. LeetCode之412. Fizz Buzz

    -------------------------------------------- 虽然是从最简单的开始刷起,但木有想到LeetCode上也有这么水的题目啊... AC代码: public cl ...

  2. VR技术的探索阶段

    转载请声明转载地址:http://www.cnblogs.com/Rodolfo/,违者必究. 早在1929年,在长期使用教练机训练器(机翼变短,不能产生离开地面所需的足够提升力)进行飞行训练之后,E ...

  3. 为何Redis要比Memcached好用(转)

    转载链接:http://blog.csdn.net/renfufei/article/details/40598889 GitHub版本地址: https://github.com/cncounter ...

  4. Django【基础篇】

    Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...

  5. 基于命令行编译打包phonegap for android应用 分类: Android Phonegap 2015-05-10 10:33 73人阅读 评论(0) 收藏

    也许你习惯了使用Eclipse编译和打包Android应用.不过,对于使用html5+js开发的phonegap应用,本文建议你抛弃Eclipse,改为使用命令行模式,绝对的快速和方便. 一直以来,E ...

  6. RESTful API 设计指南 (转)

    RESTful API 设计指南 2016-02-23 ImportNew (点击上方公号,可快速关注) 作者:阮一峰 链接:http://www.ruanyifeng.com/blog/2014/0 ...

  7. 【SSM】Eclipse使用Maven创建Web项目+整合SSM框架

    自己接触ssm框架有一段时间了,从最早的接触新版ITOO项目的(SSM/H+Dobbu zk),再到自己近期来学习到的<淘淘商城>一个ssm框架的电商项目.用过,但是还真的没有自己搭建过, ...

  8. [小工具]EquationCalcular

    名称:EquationCalcular 版本:V1.0.0 更新日期:2015/9/27   简要介绍:本工具用于计算范围比较有限的方程及方程组,仅仅局限于n元一次方程组,欢迎需要的小学生和初中生来玩 ...

  9. [Android]用图库打开指定的文件夹,没错是第一个画面直接是图库的文件夹画面

    参考了这个里面的代码 http://bbs.csdn.net/topics/380084274 一直报错 06-16 23:58:50.698 26148-26161/com.example.myap ...

  10. phonegap3.5了结

    搞了三天的phonegap3.5.最后怎么搞都搞不好了.修改了www中的index.html的内容,clean也不行,在node.js中cordova build也不行. 反正就是apk没有更新啦.至 ...