LeetCode 337
2016-05-20 21:49
197 查看
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.
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 = 0, leftMaxWithoutRoot = 0; int rightMaxWithRoot = 0, rightMaxWithoutRoot = 0; 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 = 0; int maxWithoutRoot = 0; traverse( root, &maxWithRoot, &maxWithoutRoot ); return MAX(maxWithRoot, maxWithoutRoot); }
相关文章推荐
- LeetCode 55
- 括号配对问题
- spark yarn分布式安装
- 牛客网的题,重建二叉树。(用树的先序,中序,还原二叉树)
- POJ 2253 Frogger(翻译/最短路/floyd/思维)
- Java并发教程(Oracle官方资料)
- OOM原因和解决方案
- dex文件入门(二)
- jsp 四大对象的作用域
- Java内存与垃圾收集知识总结
- ubuntu系统没有声音解决方法
- SQL SERVER 对权限的授予GRANT、拒绝DENY、收回REVOKE
- 【算法总结系列-1】快速排序-c语言实现
- THE TOWER OF HANOI
- 在一个数组中找差值最大数对,输出最大差值
- Swift-构造过程(Initialization)(十二)
- nyoj 715相邻的计数(dp)
- char *a和 char a[]
- MySQL入门——修改数据表2:往指定的表中添加多列数据(字段)
- linux awk命令详解