-
Notifications
You must be signed in to change notification settings - Fork 0
/
200926-2.cpp
74 lines (70 loc) · 1.53 KB
/
200926-2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// https://leetcode-cn.com/problems/house-robber-iii/
#include <iostream>
#include <unordered_map>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
int rob(TreeNode* root) {
unordered_map<TreeNode*, unordered_map<bool, int>> m;
return rob(root, true, m);
}
private:
int rob(TreeNode* root, bool allowRobRoot,
unordered_map<TreeNode*, unordered_map<bool, int>>& m) {
if (!root) return 0;
auto it = m.find(root);
if (it != m.end()) {
auto it2 = it->second.find(allowRobRoot);
if (it2 != it->second.end()) {
return it2->second;
}
}
int a = rob(root->left, true, m) + rob(root->right, true, m);
m[root][false] = a;
if (allowRobRoot) {
int b = rob(root->left, false, m) + root->val + rob(root->right, false, m);
if (b > a) {
a = b;
}
m[root][true] = a;
}
return a;
}
};
void release(TreeNode* t) {
if (t) {
release(t->left);
release(t->right);
delete t;
}
}
int main()
{
Solution s;
{
TreeNode* t = new TreeNode(3);
t->left = new TreeNode(2);
t->left->right = new TreeNode(3);
t->right = new TreeNode(3);
t->right->right = new TreeNode(1);
cout << s.rob(t) << endl; // answer: 7
release(t);
}
{
TreeNode* t = new TreeNode(3);
t->left = new TreeNode(4);
t->left->left = new TreeNode(1);
t->left->right = new TreeNode(3);
t->right = new TreeNode(5);
t->right->right = new TreeNode(1);
cout << s.rob(t) << endl; // answer: 9
release(t);
}
return 0;
}