二刷吧。。不知道为什么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. php 地址跳转

    header('Location: ' . $sns->getRequestCodeURL());

  2. Linux-Tcp-IP

    /* tcpcli.c */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include ...

  3. VMware网络选项分析

    摘自资料:VMware网卡选项分析.zip 很多朋友都曾问到关于Guest和Host互联,其实这并不是一件困难的事情,只要能够理解VMware的网络模型即可,今天结合着我的虚拟机,来详细介绍一下VMw ...

  4. c++ 最短路两种算法

    最短路是个老问题了,大神们留下很多文档但是很多都是针对算法使用一些固定大小的数组进行数据存储在实际应用中受到限制,这里自己练习一下,主要用了一些c++的stl,减少了固定长度数组的依赖,换一种写法试图 ...

  5. (转) 各种好用的插件 Xcode

    时间就是金钱.编码效率的提升意味着更多的收入.可是当我们的开发技巧已经到达一定高度时,如何让开发效率更上一层楼呢?答案就是使用开发工具!在这篇文章中,我会向你介绍一些帮助我提升编码速度和工作效率的工具 ...

  6. SVN - 基础知识

    1. 术语 $ svn checkout  URL [PATH] -----   下载服务器所有文件 (clone) 到本地[path]  --- 只需一次 $ svn checkout  http: ...

  7. SCOI2015题解 && 考试小结

    Day1: 第一题:裸地二分+网络流:二分答案,连接将每行每列拆成点,对于满足答案的格子行列连边,看是否流量是否大于t即可,可惜第k大看成了第k小,然后100分就没了. 第二题:倍增,考虑贪心算法,就 ...

  8. 在Eclipse中安装ADT

    启动 Eclipse,然后选择 Help > Software Updates….在出现的对话框中,单击 Available Software 选项卡. 单击 Add Site 在 Add Si ...

  9. 教你如何监控 Apache?

    什么是 Apache? Apache 是一款 HTTP 服务器软件,现在更名为 "http",而 Apache 则成了一个(包含httpd的项目)巨大的基金组织,根据习惯后文都用 ...

  10. 网站页面优化必然趋势—WebP 图片!

    本文梗概:众所周知,浏览器可以通过 HTTP 请求的 Accpet 属性 来指定接收的内容类型.依靠这个技术,可以在不修改任何 HTML/CSS 或者图片的情况下,向浏览器提供优化的图片,从而降低带宽 ...