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 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.
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
对于这个题目我们有什么思路呢?我们从根节点开始有两种选择,要么偷取根节点的值,要么不偷取根节点。偷取根节点的话,我们接下来只能偷取根节点孩子节点的孩子节点。如果不偷取根节点的话,我们可以直接偷取根节点的孩子节点。然后我们把这两种方式得到的值进行比较,取较大的那个。因此这是一个递归的过程:
rob(root) = max{rob(root.left) + rob(root.rght), root.val + rob(root.left.left) + rob(root.left.right) + rob(root.right.left) + rob(root.right.right)}
if(root == null) rob(root) = 0;
if(root.left == null && root.right == null) rob(root) = root.val
有了上面的递归表达式,我们就很容易进行编程啦!代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int rob(TreeNode root) {
if(root == null) return 0;
if(root.left == null && root.right == null) return root.val;
int maxnum1 = 0;
int maxnum2 = 0;
maxnum1 = rob(root.left) + rob(root.right);
if(root.left != null || root.right != null){
int num1 = root.left!=null?rob(root.left.left) + rob(root.left.right):0;
int num2 = root.right!=null?rob(root.right.left) + rob(root.right.right):0;
maxnum2 = num1 + num2 + root.val;
}
return maxnum1>maxnum2?maxnum1:maxnum2;
}
}
LeetCode OJ 337. House Robber III的更多相关文章
- <LeetCode OJ> 337. House Robber III
Total Accepted: 1341 Total Submissions: 3744 Difficulty: Medium The thief has found himself a new pl ...
- Leetcode 337. House Robber III
337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief 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:二叉树下的不能相邻,求能 ...
- 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] 337. House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- [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 ...
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 337. House Robber III 动态演示
每个节点是个房间,数值代表钱.小偷偷里面的钱,不能偷连续的房间,至少要隔一个.问最多能偷多少钱 TreeNode* cur mp[{cur, true}]表示以cur为根的树,最多能偷的钱 mp[{c ...
随机推荐
- redo log
1.redo log相关数据字典 v$log:display the redo log file information from the control file v$logfile:identif ...
- Activity设置singleTask无法通过Intent获取值的问题
AActivity跳转BActivity ,AActivity设置lauchmode = "SingleTask"的话,在getIntent无法获取BActivity里面的内容,无 ...
- BOS物流管理系统-第八天
BOS物流管理系统-第八天-权限系统设计-Shiro 回顾: 两大块业务:基础设置模块,业务派单模块. 两天:权限控制. 拦截器大的范围的.纯登录级别(认证级别)大颗粒的权限控制的一种技术. Shir ...
- Python编码问题整理【转】
认识常见编码 GB2312是中国规定的汉字编码,也可以说是简体中文的字符集编码 GBK 是 GB2312的扩展 ,除了兼容GB2312外,它还能显示繁体中文,还有日文的假名 cp936:中文本地系统是 ...
- Apple pay的使用
Apple pay的使用场景:1.app内:唯品会. 2.线下场景:万达 Apple pay的硬件要求:iphone6 以上 苹果婊 Apple pay的软件要求:国内(应该是)iOS9.2以上 ...
- Velocity China 2016 Web 性能与运维大会:构建快速、可扩展的弹性网站
Velocity China 2016 Web 性能与运维大会是一场关于构建快速.可扩展的弹性网站所需要的Web性能.运维及开发运维的训练.大会将于2016年12月1日在北京拉开帷幕,此次大会被众多业 ...
- CSS3秘笈复习:第七章
1.边距冲突: 当元素的bottom margin碰到另一个元素的top margin可能会产生一些怪异的计算,浏览器会忽略小的那个值而使用大的值. 2.边距折叠: 假设要在警告框里插入一个标题,并且 ...
- Testing a Redux & React web application
Part 1 - https://www.youtube.com/watch?v=iVKPbH3qyW0 Part 2 - https://www.youtube.com/watch?v=M5lwOs ...
- jquery收集--php收集所有post数据
$model = D('Account'); $data = $model->create(); jquery收集数据 sendinvite.serialize() function init ...
- iOS之网络编程
发送HTTP请求的方法 在HTTP/1.1协议中,定义了8种发送http请求的方法 GET.POST.OPTIONS.HEAD.PUT.DELETE.TRACE.CONNECT.PATCH 根据HTT ...