Leetcode 337. House Robber III
337. House Robber III
- Total Accepted: 18475
- Total Submissions: 47725
- Difficulty: Medium
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:
/ \ \ \
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
/ \ / \ \
Maximum amount of money the thief can rob = 4 + 5 = 9.
思路:基本思路和Leetcode 198. House Robber相同,只是将传递公式植入到二叉树的DFS过程中。
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void rob(TreeNode* root,int& pre,int& cur){
if(!root) return;
int lpre=,lcur=,rpre=,rcur=;
rob(root->left,lpre,lcur);
rob(root->right,rpre,rcur);
pre=lcur+rcur;
cur=max(lpre+rpre+root->val,pre);
}
int rob(TreeNode* root) {
int pre=,cur=;
rob(root,pre,cur);
return max(pre,cur);
}
};
Leetcode 337. House Robber III的更多相关文章
- [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] 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 动态演示
每个节点是个房间,数值代表钱.小偷偷里面的钱,不能偷连续的房间,至少要隔一个.问最多能偷多少钱 TreeNode* cur mp[{cur, true}]表示以cur为根的树,最多能偷的钱 mp[{c ...
- 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 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】House Robber III(337)
1. Description The thief has found himself a new place for his thievery again. There is only one ent ...
随机推荐
- 【原创】在Windows系统中使用VC9、VC11编译32位、64位PHP及其扩展
项目中需要使用runkit模块实现AOP,但是团队成员的开发环境都是Windows,而runkit模块官方没有提供Windows环境下的dll扩展,只能自己编译. 下面是编译过程的分类总结.(操作系统 ...
- Winform嵌入其它应用程序
Options: using CommandLine; using System; using System.Collections.Generic; using System.Linq; using ...
- 「NOI2014」魔法森林
题目链接 戳我 \(Solution\) 两个变量,emm...不好搞啊. 于是我们可以按照\(A\)排序.然后动态加边,因为\(A\)是越来越大,所以不需要管他,只要使得\(1\)~\(n\)的路径 ...
- HTTP协议基础(未完待续)
一.超文本传输协议 超文本传输协议(Hypertext Transfer Protocol,HTTP)是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP是万维网的数据通信的基础. 设计HT ...
- WIN7 64位配置X86 MySQL 数据源
在运行中输入“c:\windows\syswow64\odbcad32.exe”,在调出来的ODBC管理器中配置数据源.
- [HTML] 模板的用法
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...
- linux下安装nginx,centos安装nginx
初学nginx,进行简单的安装和配置. 一.依赖 openssl-fips-2.0.9.tar.gz zlib-1.2.11.tar.gz pcre-8.01.tar.gz nginx-1.8.0.t ...
- 如何在CentOS 7上使用vsftpd设置ftp服务器
一.前言介绍 FTP(文件传输协议)是一种标准的客户机-服务器网络协议,允许用户在远程网络之间传输文件. 有几个开源的FTP服务器可用于Linux.最受欢迎和广泛使用的是pureftpd.proftp ...
- VS中工程的“依赖”,“库目录”,“包含目录”
写多了Vs中的工程,就会遇到很多环境配置问题,例如“依赖项”,“库目录”,“包含目录”等等等等. 今天要记录的就是这些的基本含义:我们拿一个例子来看,更加清晰易懂一些: 例如在Opencv3.0+VS ...
- python 爬恶魔法则(单线程卡成狗)
from bs4 import BeautifulSoupimport requestsimport sysclass down(object): def __init__(self): self.n ...