二刷吧。。不知道为什么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. curl采集 根据关键词 获取雅虎竞价排名

    之前写过curl批处理采集数据,这里贴上完整版本,代码很简单,废话不说,上代码,新手欢迎指教!!! 代码只写到 获取到链接了,至于排名 后边数组的键不就是排名喽... <?php /** * B ...

  2. linq any()方法实现sql in()方法的效果

    public IQueryable<Vsec009ComSecComp> QueryList(Sec009ComSecCompQueryCondition condition) { var ...

  3. 从IT的角度思考BIM(二):模式与框架

    我们满怀着美好期许,鼓起勇气敲响了 BIM 世界的大门.忽然人群中有人高呼:BIM 已死,大家都散了吧! 这时人群开始骚动起来.“我早就说这玩意是忽悠人的吧,你们不信还偏要来”,“我花了好多钱准备这次 ...

  4. .net 反射访问私有变量和私有方法

    以下为本次实践代码: using System; using System.Collections.Generic; using System.ComponentModel; using System ...

  5. jquery点击图片选中特效

    jquery点击图片选中特效 点击在线预览效果

  6. 破解之关键CALL与关键跳查找方法

    找关键CALL和关键跳 方法一: 输入假码注册程序,记录下错误提示信息. OD载入程序--> 右键-->查找-->所有参考文本字串-->(右键-->查找文本,注:不要区分 ...

  7. Linux下fork()、vfork()、clone()和exec()的区别

    转自Linux下fork().vfork().clone()和exec()的区别 前三个和最后一个是两个类型.前三个主要是Linux用来创建新的进程(线程)而设计的,exec()系列函数则是用来用指定 ...

  8. 再探CRC

    之前写了CRC16的程序,虽说能用,却不知其所心然,现在要用CRC32,重温一遍,一下就通了.笔记如下 CRC我没记错的话是Cyclic Redundancy Code,Cyclic和Redundan ...

  9. show processlist 输出ID 和 information_schema.PROCESSLIST 的id,information_schema.innodb_trx的TRX_MYSQL_T

    Session 1: mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update Client ...

  10. 解决MySQL服务启动时报1067错误

    工具/原料 MySQL_5.6.24_win32 方法/步骤 当我们安装完Mysql时,如果在服务当中(可以在“运行”-->"service.msc"打开并查看)无法看到My ...