337. House Robber III
二刷吧。。不知道为什么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的更多相关文章
- Leetcode 337. House Robber III
337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...
- 337. House Robber III(包含I和II)
198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...
- 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:二叉树下的不能相邻,求能 ...
- [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 ...
- Java [Leetcode 337]House Robber III
题目描述: The thief has found himself a new place for his thievery again. There is only one entrance to ...
- [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 ...
- 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 ...
- 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 ...
- 337. House Robber III二叉树上的抢劫题
[抄题]: The thief has found himself a new place for his thievery again. There is only one entrance to ...
随机推荐
- 用javascript操作xml(二)JavaScript 将XML转换成字符串(xml to string)
function xmlToString(xmlData) { var xmlString; //IE if (window.ActiveXObject){ xmlString = xmlData.x ...
- html网页音乐播放器自带播放列表
基于网页的音乐播放器demo http://pan.baidu.com/s/1dDgm7HR 自己diy了一个手机端在线音乐播放器演示地址http://shanxi2014.com/zhuandiz ...
- Google v8 - Hello world
OS:Window 7 1.下载v8 zip:https://github.com/v8/v8,解压zip,重命名v8-master文件夹为v8. 2.下载安装svn:http://tortoises ...
- REST内容协商注解
@Produces注解: 用于定义方法的响应实体的数据类型.可以定义一个或多个,同时可以为每种类型定义质量因素,质量因素取值范围从0--1的小数值,默认为1. 示例: @Path("conn ...
- 个人笔记--struts2对Action的权限拦截
一.编写一个类实现com.opensymphony.xwork2.interceptor.Interceptor 接口 PermissionInterceptor.java <pre name= ...
- Java数据库连接关闭后无法启动
错误如下: java.sql.SQLException: No operations allowed after connection closed. at com.mysql.jdbc.Connec ...
- ACMer程序员智力拾遗
浏览网页偶得,遂记录下来,每天进步一点点-- 博客园真是个不错的平台,今天我让师姐也注册了-- 学会分享吧,孩子们-- 一.编程中无穷大量的设置 ...
- 【2011 Greater New York Regional 】Problem G: Rancher's Gift
计算几何的题目,很简单: 自己随手敲了个,纪念下! #include<cstdio> #include<cmath> using namespace std; struct p ...
- CF_Lucky Sum
幸运数字的定义是这样:仅含4和7且不比n小的数为n的幸运数字. 输入范围l,r要求输出这个范围内的数字的幸运数字之和. 代码: #include<stdio.h> #define N 10 ...
- [转贴]sizeof 和strlen的区别
1. 编译时计算运算符sizeof,可用类型或变量做参数,计算占用内存的大小.sizeof后若是类型必须加括弧,若是变量名可不加括弧.sizeof(x)可用来定义数组维数.如:printf(" ...