-
Notifications
You must be signed in to change notification settings - Fork 28
/
House_Robber_III.cpp
44 lines (39 loc) · 1.19 KB
/
House_Robber_III.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
/**
* 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 {
int robUtil(TreeNode* root, int& nxt) {
if(!root) { nxt = 0; return 0; }
int nxt1 = 0, nxt2 = 0;
int leftSum = robUtil(root->left, nxt1);
int rightSum = robUtil(root->right, nxt2);
nxt = max(leftSum, nxt1) + max(rightSum, nxt2);
return nxt1 + root->val + nxt2;
}
public:
int rob(TreeNode* root) {
int result2;
int result = robUtil(root, result2);
return max(result, result2);
}
};
// return using pair
class Solution {
pair<int, int> robUtil(TreeNode* root) {
if(!root) { return {0, 0}; }
auto leftSum = robUtil(root->left);
auto rightSum = robUtil(root->right);
return {leftSum.second + root->val + rightSum.second, max(leftSum.first, leftSum.second) + max(rightSum.first, rightSum.second)};
}
public:
int rob(TreeNode* root) {
pair<int, int> result = robUtil(root);
return max(result.first, result.second);
}
};