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 area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
Example 1:
3
/ \
2 3
\ \
3 1
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
3
/ \
4 5
/ \ \
1 3 1
Maximum amount of money the thief can rob = 4 + 5 = 9.
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def rob(self, root):
"""
:type root: TreeNode
:rtype: int
3 => 3
3 => 3
/
2
3
\
3 =>3=max(3,3)
3
/ \ => 3+2=5=max(3, 2+3)
2 3
3
/ \
2 3
\
3 => 3+3=6=max(3+3,2+3)=max(3+node2,3 not choose, node2,3 choosed)
1
/ \
4 1
/ \ \
1 1 5 => 4+5=9=max(3+3+1+1,4+5)=9
"""
return max(self.rob_helper(root)) def rob_helper(self, root):
if root is None:
return [0, 0]
ans = [0]*2
ans_left = self.rob_helper(root.left)
ans_right = self.rob_helper(root.right)
ans[0] = max(ans_left[0], ans_left[1]) + max(ans_right[0], ans_right[1])
ans[1] = ans_left[0] + ans_right[0] + root.val
return ans
337. House Robber III——树的题目几乎都是BFS、DFS,要么递归要么循环的更多相关文章
- 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 ...
- 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 ...
- LeetCode 337. House Robber III 动态演示
每个节点是个房间,数值代表钱.小偷偷里面的钱,不能偷连续的房间,至少要隔一个.问最多能偷多少钱 TreeNode* cur mp[{cur, true}]表示以cur为根的树,最多能偷的钱 mp[{c ...
随机推荐
- STORM_0006_第二个storm_topology:WordCountTopology的代码与运行
我先试试这个Open Live Writer能不能用. 再在ScribeFire中修改一下已经发布的文章试试看. 这两个写博客的地方都没有原始的编辑器方便,可以插入代码,选择文章的分类.所以以后还有这 ...
- C++中的一些定义
PS: 这篇博客用来记录一些一般的C++书中草草掠过的一些概念. 或者一些不太容易理解的概念的详细解释. 欢迎新手进入,欢迎高手指正! Orz . 引用: 为对象起了另外一个名字, 引用类型引用(re ...
- winform打开唯一窗体、构造函数传值
制作登入窗体: 制作一个登陆窗体,实现点击按钮关闭此窗体并打开另一个窗体 直接在按钮点击事件中,实例化一个想要打开的窗体 使用show方法打开,并把登陆窗体的visible属性改为false Form ...
- 12/09 Oracle练习之新建表
- 【Todo】Lucene系统学习
之前已经写过一篇关于Lucene安装学习的文章:http://www.cnblogs.com/charlesblc/p/5980525.html 还有一篇关于Solr安装使用的文章:http://ww ...
- HTML常用的特殊字符格式
空格: 版权号:©注册商标:®":quot; ':'&:&<:<>:> ©:© «:« ®:® ¯:¯ ¼:¼ (14表示四分之一 ...
- Vbs脚本经典教材(转)
Vbs脚本经典教材(最全的资料还是MSDN) —为什么要使用Vbs? 在Windows中,学习计算机操作也许很简单,但是很多计算机工作是重复性劳动,例如你每周也许需要对一些计算机文件进行复制.粘贴.改 ...
- Linux计划任务Crontab实例详解教程
说明:Crontab是Linux系统中在固定时间执行某一个程序的工具,类似于Windows系统中的任务计划程序 下面通过详细实例来说明在Linux系统中如何使用Crontab 操作系统:CentOS ...
- 多线程调用HttpWebRequest并发连接限制
.net 的 HttpWebRequest 或者 WebClient 在多线程情况下存在并发连接限制,这个限制在桌面操作系统如 windows xp , windows 7 下默认是2,在服务器操作 ...
- php防止sql注入
[一.在服务器端配置] 安全,PHP代码编写是一方面,PHP的配置更是非常关键. 我们php手手工安装的,php的默认配置文件在 /usr/local/apache2/conf/php.ini,我们最 ...