337. House Robber III
2016-03-27 21:35
176 查看
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.
分析:
下面的代码我觉得很奇妙,抓不住其中的道理,递归的概念还是不够清晰。
对于每一个节点,都可以有两种的结果,1自己+grandnode的vals,2左子树的节点值加右子树的值。
也算动态规划吧,只是做法不同一样。
/**
* 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 tryRob(TreeNode* root, int &l,int &r)
{
if(root==NULL) return 0;
int ll=0,lr=0,rl=0,rr=0;
l=tryRob(root->left,ll,lr);
r=tryRob(root->right,rl,rr);
return max(root->val+ll+lr+rl+rr,l+r);
}
int rob(TreeNode* root) {
int l, r;
return tryRob(root, l, r);
}
};
上面解法的精髓在于不会计算两次,因此很不好理解。写了个简单版本的,这个版本比较好理解,但的确计算成本太高,
代码:
/**
* 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;
}
if(root->left==NULL&&root->right==NULL){
return root->val;
}
int leftval = 0;
int leftonly = 0;
int rightval = 0;
int rightonly = 0;
if(root->left){
leftval = rob(root->left->left)+rob(root->left->right);
leftonly= rob(root->left);
}
if(root->right){
rightval = rob(root->right->left)+rob(root->right->right);
rightonly= rob(root->right);
}
return max((root->val+leftval+rightval),leftonly+rightonly);
}
};
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.
分析:
下面的代码我觉得很奇妙,抓不住其中的道理,递归的概念还是不够清晰。
对于每一个节点,都可以有两种的结果,1自己+grandnode的vals,2左子树的节点值加右子树的值。
也算动态规划吧,只是做法不同一样。
/**
* 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 tryRob(TreeNode* root, int &l,int &r)
{
if(root==NULL) return 0;
int ll=0,lr=0,rl=0,rr=0;
l=tryRob(root->left,ll,lr);
r=tryRob(root->right,rl,rr);
return max(root->val+ll+lr+rl+rr,l+r);
}
int rob(TreeNode* root) {
int l, r;
return tryRob(root, l, r);
}
};
上面解法的精髓在于不会计算两次,因此很不好理解。写了个简单版本的,这个版本比较好理解,但的确计算成本太高,
代码:
/**
* 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;
}
if(root->left==NULL&&root->right==NULL){
return root->val;
}
int leftval = 0;
int leftonly = 0;
int rightval = 0;
int rightonly = 0;
if(root->left){
leftval = rob(root->left->left)+rob(root->left->right);
leftonly= rob(root->left);
}
if(root->right){
rightval = rob(root->right->left)+rob(root->right->right);
rightonly= rob(root->right);
}
return max((root->val+leftval+rightval),leftonly+rightonly);
}
};
相关文章推荐
- 命令行快速技巧:如何定位一个文件
- 文件遍历排序函数
- Lua 学习笔记之C API 遍历 Table实现代码
- C#遍历文件夹后上传文件夹中所有文件错误案例分析
- C#中遍历Hashtable的4种方法
- jquery+CSS实现的多级竖向展开树形TRee菜单效果
- Erlang中遍历取出某个位置的最大值代码
- C++实现图的邻接矩阵存储和广度、深度优先遍历实例分析
- C++实现图的邻接表存储和广度优先遍历实例分析
- C++非递归队列实现二叉树的广度优先遍历
- php遍历目录方法小结
- 一个目录遍历函数
- php遍历删除整个目录及文件的方法
- PHP遍历文件夹与文件类及处理类用法实例
- PHP遍历XML文档所有节点的方法
- php中使用key,value,current,next和prev函数遍历数组的方法
- C#使用前序遍历、中序遍历和后序遍历打印二叉树的方法
- C#使用foreach遍历哈希表(hashtable)的方法
- php递归遍历多维数组的方法
- C#使用yield关键字让自定义集合实现foreach遍历的方法