Skip to content

Commit 97389ff

Browse files
authored
House Robber III
1 parent 3393ea0 commit 97389ff

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

House Robber III.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
unordered_map<TreeNode*,int> um;
13+
14+
int rob(TreeNode* root) {
15+
if(root==NULL)
16+
return 0;
17+
if(um.find(root)!=um.end())
18+
return (um.find(root)->second);
19+
int val=0;
20+
if(root->left!=NULL)
21+
val+=rob(root->left->left) + rob(root->left->right);
22+
if(root->right!=NULL)
23+
val+=rob(root->right->left) + rob(root->right->right);
24+
int temp=max(val+root->val, rob(root->left)+rob(root->right));
25+
26+
um[root]=temp;
27+
return temp;
28+
}
29+
};

0 commit comments

Comments
 (0)