-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_337_HouseRobberIII.cpp
45 lines (39 loc) · 1.23 KB
/
LC_337_HouseRobberIII.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
/*
https://leetcode.com/problems/house-robber-iii/
337. House Robber III
*/
/**
* 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 {
public:
int rob(TreeNode* root) {
if(!root)
return 0;
if(!root->left && !root->right)
return root->val;
// auto [curr, last] = solve(root);
// return curr;
return solve(root).first;
}
pair<int,int> solve(TreeNode* root)
{
if(root == nullptr)
return {0, 0};
if(!root->left && !root->right) // if leaf node then {max of last node, max of second last node}
return {root->val, 0};
auto [last_left, seclast_left] = solve(root->left);
auto [last_right, seclast_right] = solve(root->right);
int curr = root->val + seclast_left + seclast_right;
int last = last_left + last_right;
return {max(curr, last), last};
}
};