<LeetCode OJ> 337. House Robber III
Submissions: 3744 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:
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.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int rob(TreeNode* root) {
if(root==NULL)
return 0;
queue<TreeNode*> que;//用来总是保存当层的节点
que.push(root);
int oddsum =root->val;//用于统计奇数层的和
int evensum=0; //用于统偶数层的和
//获取每一层的节点
int curlevel=2;
while(!que.empty())
{
int levelSize = que.size();//通过size来推断当层的结束
for(int i=0; i<levelSize; i++)
{
if(que.front()->left != NULL) //先获取该节点下一层的左右子,再获取该节点的元素。由于一旦压入必弹出。所以先处理左右子
que.push(que.front()->left);
if(que.front()->right != NULL)
que.push(que.front()->right);
if(curlevel % 2 ==1)
oddsum += que.front()->val;
else
evensum += que.front()->val;
que.pop();
}
curlevel++;
}
return oddsum > evensum ? oddsum : evensum;//奇数层的和与偶数层的和谁更大谁就是结果
}
};
学习别人的代码:
int rob(TreeNode* root) {
int child = 0, childchild = 0;
rob(root, child, childchild);
return max(child, childchild);
} void rob(TreeNode* root, int &child, int &childchild) {
if(!root) return; int l1 = 0, l2 = 0, r1 = 0, r2 = 0;
rob(root->left, l1, l2);
rob(root->right, r1, r2); child = l2 + r2 + root->val;
childchild = max(l1, l2) + max(r1, r2);
}
注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50890931
原作者博客:http://blog.csdn.net/ebowtang
本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895
<LeetCode OJ> 337. House Robber III的更多相关文章
- 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 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 ...
- [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 ...
随机推荐
- JDBC 学习笔记(二)—— 详解 JDBC 的四种驱动类型
JDBC 有四种驱动类型,分别是: JDBC-ODBC 桥(JDBC-ODBC bridge driver plus ODBC driver) 本地 API 驱动(Native-API partly ...
- 在Ubuntu中,用mvn打包hadoop源代码时报错,正在解决中!!!
报错信息如下: (各种配置在最后面) hadoop@administrator-virtual-machine:~/Downloads/tar/hadoop-3.0.0-alpha1-src$ mvn ...
- BZOJ3991 [SDOI2015]寻宝游戏 【dfs序 + lca + STL】
题目 小B最近正在玩一个寻宝游戏,这个游戏的地图中有N个村庄和N-1条道路,并且任何两个村庄之间有且仅有一条路径可达.游戏开始时,玩家可以任意选择一个村庄,瞬间转移到这个村庄,然后可以任意在地图的道路 ...
- 刷题总结——pole(uva 1638 dp)
题目: 题解: 这道题很妙的一点是很好地利用了最矮的杆子除了放两侧以外对观察数是没有影响的性质·· 考虑n-1个杆子与n个杆子··我们可以把n个杆子的排列看成n-1个杆子的长度加1按原来的排列顺序·· ...
- 刷题总结——维护数列(NOI2005 bzoj1500 splay)
题目: 题目背景 NOI2005 DAY1 T2 题目描述 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏中的下划线‘_’表示实际输入文件中的空格)
- 欧拉回路 & 欧拉路径
欧拉路径 & 欧拉回路 概念 欧拉路径: 如果图 G 种的一条路径包括所有的边,且仅通过一次的路径. 欧拉回路: 能回到起点的欧拉路径. 混合图: 既有无向边又有无向边的图. 判定 无向图 一 ...
- bzoj 3779 重组病毒 好题 LCT+dfn序+线段树分类讨论
题目大意 1.将x到当前根路径上的所有点染成一种新的颜色: 2.将x到当前根路径上的所有点染成一种新的颜色,并且把这个点设为新的根: 3.查询以x为根的子树中所有点权值的平均值. 分析 原题codec ...
- Bzoj2038 小Z的袜子(hose)
Time Limit: 20000MS Memory Limit: 265216KB 64bit IO Format: %lld & %llu Description 作为一个生活散漫 ...
- windows命令行中java和javac、javap使用详解(java编译命令)
如题,首先我们在桌面,开始->运行->键入cmd 回车,进入windows命令行.进入如图所示的画面: 可知,当前默认目录为C盘Users文件夹下的Administrator文件夹.一般而 ...
- x86 下的 struct 變數 member 擺放位置
2 int main() 3 { 4 struct _test { 5 int a; 6 int b; 7 int c; 8 }; 9 10 struct _test test; 11 test.a ...