二刷吧。。不知道为什么house robbery系列我找不到笔记,不过印象中做了好几次了。

不是很难,用的post-order做bottom-up的运算。

对于一个Node来说,有2种情况,一种是选(自己+下下层);一种是选左右children.

其实就是选自己和不选自己的区别。其实更像是dfs的题而不是DP的题。

Time: O(n)

Space: O(lgn)

public class Solution {
public int rob(TreeNode root) {
if (root == null) return 0; int leftLevel = 0;
int leftSubLevel = 0;
if (root.left != null) {
leftLevel = rob(root.left);
leftSubLevel = rob(root.left.left) + rob(root.left.right);
} int rightLevel = 0;
int rightSubLevel = 0;
if (root.right != null) {
rightLevel = rob(root.right);
rightSubLevel = rob(root.right.left) + rob(root.right.right);
} return Math.max((root.val + leftSubLevel + rightSubLevel), leftLevel + rightLevel);
}
}

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. 337. House Robber III(包含I和II)

    198. House Robber You are a professional robber planning to rob houses along a street. Each house 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] 337. House Robber III 打家劫舍之三

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

  5. Java [Leetcode 337]House Robber III

    题目描述: The thief has found himself a new place for his thievery again. There is only one entrance to ...

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

  7. 337. House Robber III——树的题目几乎都是BFS、DFS,要么递归要么循环

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

  8. LeetCode OJ 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. 337. House Robber III二叉树上的抢劫题

    [抄题]: The thief has found himself a new place for his thievery again. There is only one entrance to ...

随机推荐

  1. 进程显示,删除,调度 ps, top kill

    ps:查看进程的情况,显示的是某一时间进程的运行状态.ps --help top:也是查看进程的情况,动态显示进程信息! kill:杀死进程的情况, sudo kill --help 查看相关参数 c ...

  2. React组件三

    <script> <!-- getDefalutPros 设置组件的默认值--> <!--var Mytitle=React.createClass({ getDefau ...

  3. 阻止CSS样式被缓存

    <link href="/stylesheet.css?<?php echo time(); ?>" rel="stylesheet" typ ...

  4. windows下Apache配置SSL安全连接

    什么是SSL? SSL(Secure Socket Layer): 是为Http传输提供安全的协议,通过证书认证来确保客户端和网站服务器之间的数据是安全.Open SSL下载地址:http://www ...

  5. tomcat https 配置

    以前基本上笔者对于安全性考虑的并不多,最近因为saas平台要开始逐渐推广,所以需要开始逐渐加强xss/crsf/https等措施以避免潜在的安全性风险.本文简单的记录下tomcat下https的配置. ...

  6. 关于django Models的个人理解和related_name的使用

    作为一个新人(刚刚大学还没有毕业就出来实习,可以说是真的什么都不知到,什么都要重新学,但是这样真的可以锻炼自己的意志力和能力).现在在公 司是前端和后端一起坐,所以要学的东西是真的多的让人想不到.在学 ...

  7. Android 使用SharedPreference来进行软件配置的存取

    我们在安卓开发的时候不免需要记录用户键入的一些信息,比如账号和密码,用户使用软件的次数,上次打开软件的时间等等,为了保存这些配置,我们可以使用SharedPreference类保存他们. //使用Sh ...

  8. [wikioi]回家

    http://wikioi.com/problem/1079/ 单源最短路径,可以用dijkstra来做.这里采用了heap优化,复杂度是(V+E)logV.这里用了STL的优先队列(堆),重复加入p ...

  9. [topcoder]BinaryCards

    现在觉得有空时可以刷一下topcoder的DIV 2的Lvl 3的题目.感觉和刷LeetCode和WikiOi都是不一样的. http://community.topcoder.com/stat?c= ...

  10. MVVM_Android-CleanArchitecture

    前言 "Architecture is About Intent, not Frameworks" - Robert C. Martin (Uncle Bob) Uncle Bob ...