LeetCode#337. House Robber III
2016-03-16 12:56
337 查看
337. House Robber III
Total Accepted: 2052 Total Submissions: 5607 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.2052 Total Submissions: 5607 Difficulty: Medium
解题思路
(1)DFS:深度优先搜索
(2)递归函数返回两个值,分别是窃取root节点和不窃取root节点时窃取的资金总额。
当根节点为空时,直接返回(0,0).
根节点不为空的时候,窃取根节点的时候,窃取的资金为left[0] + right[0] + root->val(此时,不能窃取根节点左右子结点).
而不窃取根节点的时候,窃取的资金为max(left[0], left[1]) + max(right[0], right[1]),即左右子节点能够窃取的最大值之和。
(3)最终我们返回窃取root节点和不窃取root节点偷得的资金的最大值。
C++代码
/**
* 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) {
vector<int> ans = dfs(root);
return max(ans[0], ans[1]);
}
vector<int> dfs(TreeNode *root) {
if (!root) return vector<int>(2, 0);//当前根节点为空
vector<int> left = dfs(root->left);//递归深度优先搜索左子树
vector<int> right = dfs(root->right);//递归深度优先搜索右子树
vector<int> ans(2, 0);
ans[0] = max(left[0], left[1]) + max(right[0], right[1]);
ans[1] = left[0] + right[0] + root->val;
return ans;
}
};
Total Accepted: 2052 Total Submissions: 5607 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.2052 Total Submissions: 5607 Difficulty: Medium
解题思路
(1)DFS:深度优先搜索
(2)递归函数返回两个值,分别是窃取root节点和不窃取root节点时窃取的资金总额。
当根节点为空时,直接返回(0,0).
根节点不为空的时候,窃取根节点的时候,窃取的资金为left[0] + right[0] + root->val(此时,不能窃取根节点左右子结点).
而不窃取根节点的时候,窃取的资金为max(left[0], left[1]) + max(right[0], right[1]),即左右子节点能够窃取的最大值之和。
(3)最终我们返回窃取root节点和不窃取root节点偷得的资金的最大值。
C++代码
/**
* 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) {
vector<int> ans = dfs(root);
return max(ans[0], ans[1]);
}
vector<int> dfs(TreeNode *root) {
if (!root) return vector<int>(2, 0);//当前根节点为空
vector<int> left = dfs(root->left);//递归深度优先搜索左子树
vector<int> right = dfs(root->right);//递归深度优先搜索右子树
vector<int> ans(2, 0);
ans[0] = max(left[0], left[1]) + max(right[0], right[1]);
ans[1] = left[0] + right[0] + root->val;
return ans;
}
};
相关文章推荐
- C++动态规划之最长公子序列实例
- C++动态规划之背包问题解决方法
- Win2003利用dfs(分布式文件系统)在负载均衡下的文件同步配置方案
- C#使用动态规划解决0-1背包问题实例分析
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- win2003分布式文件系统(dfs)配置方法[图文详解]
- win2003分布式文件系统及其部署 图文教程
- LFC1.0.0 版本发布
- 动态规划
- Hadoop2.6+jdk8的安装部署(1)——使用jar包安装部署【详细】
- Hadoop FS Shell
- C++ 动态规划
- DFS使用方法总结
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- FastDFS注意事项
- 无忧技术带您预览DFS(分布式文件系统)管理控制台
- android中像素单位dp、px、pt、sp的比较