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 ...
随机推荐
- mysql中char与varchar的区别
在建立数据库表结构的时候,为了给一个String类型的数据定义一个数据库的数据库类型,一般参考的都是char或者varchar,这两种选择有时候让人很纠结,今天想总结一下它们两者的区别,明确一下选择塔 ...
- 如何使用KMS激活win10和office
首先你需要下载一个kms软件,地址: https://yunpan.cn/cRxVNy2LRXjBt (提取码:d5d8) 然后搭建kms服务器,很简单.启动软件,选择“附加”Tab, 点连接到服务器 ...
- ARM编译器4字节对齐
(1)我们假设只有一个赋初值的char型全局变量,那么系统会在data区分配一个4字节的存储空间来存储它.实际上,只用了1个字节,但是为了4字节对齐,只好分配4个字节,所以就会有3个字节浪费. (2) ...
- Python中类的运算符重载
这篇文章仅仅是总结性质的,待以后有时间的时候会针对比较难理解的部分补充一些例子. 构造和析构 __init__ __del__ 函数调用 __call__ 打印操作 __str__ __repr__ ...
- Firebird/InterBase内置函数使用说明
Firebird/InterBase内置函数使用说明(转自:圣域天堂) 2008-10-12 20:56 加*号为FB2.0加入的函数 整理:剑雷(jianlei) 2006-10-13 1. COU ...
- unidac连接FireBird数据库
dbconn: TUniConnection; with dbconn do begin if not Connected then begin ...
- bzoj 3823: 定情信物 线性筛逆元
3823: 定情信物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 108 Solved: 2[Submit][Status] Descriptio ...
- Word里插入表格不带左右边框
插入表格后选中,然后开始-----段落------选择右下角的边框设置,选择无左右边框.
- GITLAB的版本回退(非命令行)
今天遇到小韩的问题,大约解决如下:
- 【LA 5713 】 Qin Shi Huang's National Road System (MST)
[题意] 秦始皇要在n个城市之间修路,而徐福声可以用法术位秦始皇免费修1条路,每个城市还有人口数,现要求徐福声所修之路的两城市的人口数之和A尽量大,而使n个城市互通需要修的路长B尽量短,从而使得A/B ...