-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rob.cpp
42 lines (35 loc) · 1.25 KB
/
rob.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
int robHelper(TreeNode* node, unordered_map<TreeNode*, int>& robMap) {
if(node == NULL) return 0;
if(robMap.count(node) > 0) return robMap.at(node);
int withoutNode = robHelper(node->left, robMap) + robHelper(node->right, robMap);
int withNode = node->val;
if(node->left) {
withNode += robHelper(node->left->left, robMap);
withNode += robHelper(node->left->right, robMap);
}
if(node->right) {
withNode += robHelper(node->right->left, robMap);
withNode += robHelper(node->right->right, robMap);
}
robMap[node] = max(withNode, withoutNode);
return robMap[node];
}
public:
int rob(TreeNode* root) {
unordered_map<TreeNode*, int> robMap;
return robHelper(root, robMap);
}
};