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

3
 / \
2   3
 \   \
  3   1

Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

3
   / \
  4   5
 / \   \
1   3   1

Maximum amount of money the thief can rob = 4 + 5 = 9.

LeetCode上的原题,请参见我之前的博客House Robber III

解法一:

class Solution {
public:
int houseRobber3(TreeNode* root) {
unordered_map<TreeNode*, int> m;
return helper(root, m);
}
int helper(TreeNode *root, unordered_map<TreeNode*, int> &m) {
if (!root) return ;
if (m.count(root)) return m[root];
int val = ;
if (root->left) {
val += helper(root->left->left, m) + helper(root->left->right, m);
}
if (root->right) {
val += helper(root->right->left, m) + helper(root->right->right, m);
}
val = max(val + root->val, helper(root->left, m) + helper(root->right, m));
m[root] = val;
return val;
}
};

解法二:

class Solution {
public:
int houseRobber3(TreeNode* root) {
vector<int> res = helper(root);
return max(res[], res[]);
}
vector<int> helper(TreeNode *root) {
if (!root) return {, };
vector<int> left = helper(root->left);
vector<int> right = helper(root->right);
vector<int> res{, };
res[] = max(left[], left[]) + max(right[], right[]);
res[] = left[] + right[] + root->val;
return res;
}
};

[LintCode] House Robber III 打家劫舍之三的更多相关文章

  1. [LeetCode] House Robber III 打家劫舍之三

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  2. [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 ...

  3. [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 ...

  4. [LintCode] House Robber II 打家劫舍之二

    After robbing those houses on that street, the thief has found himself a new place for his thievery ...

  5. 337 House Robber III 打家劫舍 III

    小偷又发现一个新的可行窃的地点. 这个地区只有一个入口,称为“根”. 除了根部之外,每栋房子有且只有一个父房子. 一番侦察之后,聪明的小偷意识到“这个地方的所有房屋形成了一棵二叉树”. 如果两个直接相 ...

  6. 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:二叉树下的不能相邻,求能 ...

  7. [LeetCode] House Robber II 打家劫舍之二

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  8. Leetcode 337. House Robber III

    337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...

  9. 337. House Robber III(包含I和II)

    198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...

随机推荐

  1. RDS MySQL 全文检索相关问题的处理

    RDS MySQL 全文检索相关问题 1. RDS MySQL 对全文检索的支持 2. RDS MySQL 全文检索相关参数 3. RDS MySQL 全文检索中文支持 3.1 MyISAM 引擎表 ...

  2. ado.net增删改查操作

    ado.net是数据库访问技术将数据库中的数据,提取到内存中,展示给用户看还可以将内存中的数据写入数据库中去 并不是唯一的数据库访问技术,但是它是最底层最基础的数据库访问技术 使用ado.net对数据 ...

  3. 587A

    #include<iostream> #include<algorithm> #include<stdio.h> #include<stdlib.h> ...

  4. 用js完成毫秒格式数据的日期格式化任务

    后台传过来的数据  creationTime  在后台是Date类型的 毫秒转换成  05-24  月 日格式的 //获得月日得到日期oTime function getMoth(str){ var  ...

  5. 手机页面的meta标签

    <meta charset="utf-8"/><meta name="viewport" content="width=device ...

  6. ++i与i++的区别

    1. ++i 和 i++,在单独使用时,就是 i=i+1. 2. a = ++i,相当于 i=i+1; a = i; (先i = i + 1,再使用i的值).也可以写成 i++; a=i 3. a = ...

  7. HDU 3926 图的同构

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3926 题意:给定2个顶点度最大为2的无向图.问你这2个无向图是否同构. 思路: 1.最大度为2.说明这 ...

  8. loopback 05

    数据并发处理 数据库事务 事务隔离 ACID性质 原子性(Atomicity): 要么全部被执行,要么都不执行; 一致性(Consistency): 满足完整性约束; 隔离性(Isolation): ...

  9. css整理-05 边框,背景和浮动,定位

    边框 样式:border-style hidden, dotted, dashed, solid , double, groove, ridge, inset, outset 最不可预测的是doubl ...

  10. http://www.cnblogs.com/meiCode/p/5896239.html

    http://www.cnblogs.com/meiCode/p/5896239.html