LeetCode 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.
/*************************************************************************
> File Name: LeetCode337.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Wed 11 May 2016 19:08:25 PM CST
************************************************************************/ /************************************************************************* 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. ************************************************************************/ #include <stdio.h> /*
继198与213
*/ /*
Discuss区大神答案
https://leetcode.com/discuss/91777/intuitive-still-efficient-solution-accepted-well-explained
另外有C++详解
https://leetcode.com/discuss/91899/step-by-step-tackling-of-the-problem
*/ /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/ #define MAX(a, b) ((a) > (b) ? (a) : (b)) // struct TreeNode
// {
// int val;
// struct TreeNode *left;
// struct TreeNode *right;
// }; void traverse( struct TreeNode* root, int* maxWithRoot, int* maxWithoutRoot )
{
int leftMaxWithRoot = , leftMaxWithoutRoot = ;
int rightMaxWithRoot = , rightMaxWithoutRoot = ; if( root )
{
traverse( root->left, &leftMaxWithRoot, &leftMaxWithoutRoot );
traverse( root->right, &rightMaxWithRoot, &rightMaxWithoutRoot ); *maxWithRoot = leftMaxWithoutRoot + rightMaxWithoutRoot + root->val;
*maxWithoutRoot = MAX( leftMaxWithRoot, leftMaxWithoutRoot ) + MAX( rightMaxWithRoot, rightMaxWithoutRoot );
}
} int rob(struct TreeNode* root)
{
int maxWithRoot = ;
int maxWithoutRoot = ; traverse( root, &maxWithRoot, &maxWithoutRoot ); return MAX(maxWithRoot, maxWithoutRoot);
}
LeetCode 337的更多相关文章
- Leetcode 337. House Robber III
337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief 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. 打家劫舍 III
题目链接 https://leetcode.com/problems/house-robber-iii/description/ 题目描述 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可 ...
- [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 打家劫舍 III(三)
337. 打家劫舍 III 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区.这个地区只有一个入口,我们称之为"根". 除了"根"之外,每 ...
- [LeetCode] 337. 打家劫舍 III (树形dp)
题目 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区.这个地区只有一个入口,我们称之为"根". 除了"根"之外,每栋房子有且只有一个&q ...
- 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.大家结舍III
打家劫舍III 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区.这个地区只有一个入口,我们称之为"根".除了"根"之外,每栋房子有且只有 ...
- 【LeetCode 337 & 329. memorization DFS】House Robber III
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
随机推荐
- effective c++ (一)
条款01:把C++看作一个语言联邦 C++是一种多重范型编程语言,一个同时支持过程(procedural),面向对象(object-oriented),函数形式(functional),泛型形式(ge ...
- js打开新页面 关闭当前页 关闭父页面
js打开新页面.关闭当前页.关闭父页面 2010-04-29 14:04:13| 分类: 页面与JavaScript | 标签: |字号大中小 订阅 //关闭当前页面,并且打开新页面,(不 ...
- 对Slony-I中wait on的理解
http://slony.info/documentation/2.1/advanced.html#AEN1425 4.1.2. Event Confirmations When an event i ...
- Java面试试题
第一,谈谈final, finally, finalize的区别.最常被问到. 第二,Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以impl ...
- 微价值:专訪《甜心爱消除》个人开发人员Lee,日入千元!
[导语]我们希望能够对一些个人开发人员进行专訪,这样大家更能显得接地气,看看人家做什么,怎么坚持.<甜心爱消除>作者Lee是三群的兄弟,也关注微价值.微价值的文章还是能够的,得到一些业内大 ...
- 栈上连续定义的int变量,地址相差12个字节
在VS2010,进行调试的时候,发现连续定义的int变量,地址相差12个字节.这是为什么? 按照我们的理解,int占用4个字节,应该相差4个字节.这是因为VS2010在Debug模式下,int变量占用 ...
- C++ 名称空间
在程序中,只使用 using namespace std; 而不使用其他的名称空间,如using namespace boost; 这样的好处有: 1.可以避免不同名称空间中的名称冲突: 2.可以很清 ...
- Android Studio系列教程一--下载与安装
背景 相信大家对Android Studio已经不陌生了,Android Studio是Google于2013 I/O大会针对Android开发推出的新的开发工具,目前很多开源项目都已经在采用,Goo ...
- influxdb ERR: error parsing query: found -, expected
ERR: error parsing query: found -, expected 使用时遇到这个问题,执行语句: select * FROM test10-cc-core01 本来以为和sql语 ...
- 第七届ACM趣味程序设计竞赛第四场(正式赛) 题解
Final Pan's prime numbers 题目连接: http://acm.uestc.edu.cn/#/problem/show/1272 题意 给你n,要求你在[4,n]范围内找到一个最 ...