-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_1373_MaxSumBSTinBT.cpp
95 lines (79 loc) · 2.56 KB
/
LC_1373_MaxSumBSTinBT.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/
1373. Maximum Sum BST in Binary Tree
*/
/**
* 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 NodeValue
{
public:
int minval, maxval, sum;
NodeValue(int mini, int maxi, int s)
{
minval = mini;
maxval = maxi;
sum = s;
}
};
class Solution {
public:
int ans=0;
int maxSumBST(TreeNode* root) {
if(!root)
return 0;
checkMaxSumBST(root);
return ans;
}
NodeValue checkMaxSumBST(TreeNode* root)
{
if(!root)
return NodeValue(INT_MAX, INT_MIN, 0);
if(!root->left and !root->right)
{
ans = max(ans, root->val);
return NodeValue(root->val, root->val, root->val);
}
auto left = checkMaxSumBST(root->left);
auto right = checkMaxSumBST(root->right);
NodeValue retnode(INT_MIN, INT_MAX, 0);
if(left.maxval >= root->val or right.minval <= root->val)
return retnode;
retnode.sum = root->val + left.sum + right.sum;
retnode.minval = min(left.minval, root->val);
retnode.maxval = max(right.maxval, root->val);
ans = max(ans, retnode.sum);
return retnode;
}
// typedef vector<int> vi;
// // minval and maxval is to check for bst while sum is to calculate for bst key sum
// enum ids {minval, maxval, sum};
// vi checkMaxSumBST(TreeNode* root)
// {
// if(!root)
// return {INT_MAX, INT_MIN, 0};
// if(!root->left and !root->right)
// {
// ans = max(ans, root->val);
// return {root->val, root->val, root->val};
// }
// auto left = checkMaxSumBST(root->left);
// auto right = checkMaxSumBST(root->right);
// vi retvec = {INT_MIN, INT_MAX, 0};
// if(left[maxval] >= root->val or right[minval] <= root->val)
// return retvec;
// retvec[sum] = root->val + left[sum] + right[sum];
// retvec[minval] = min(left[minval], root->val);
// retvec[maxval] = max(right[maxval], root->val);
// ans = max(ans, retvec[sum]);
// return retvec;
// }
};