-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1373_maxSumBSTinBinaryTree.cpp
44 lines (41 loc) · 1.34 KB
/
1373_maxSumBSTinBinaryTree.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() : 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 minNode, maxNode, maxSum;
NodeValue(int minNode, int maxNode, int maxSum){
this->minNode = minNode;
this->maxNode = maxNode;
this->maxSum = maxSum;
}
};
class Solution {
NodeValue maxSumBSThelper(TreeNode* root, int &ans){
if(!root){
return NodeValue(INT_MAX, INT_MIN, 0);
}
auto left = maxSumBSThelper(root->left, ans);
auto right = maxSumBSThelper(root->right, ans);
if(left.maxNode < root->val && right.minNode > root->val){
//valid BST
ans = max(ans, root->val + right.maxSum + left.maxSum);
return NodeValue(min(left.minNode, root->val), max(root->val, right.maxNode), root->val + right.maxSum + left.maxSum);
}
return NodeValue(INT_MIN, INT_MAX, max(left.maxSum, right.maxSum));
}
public:
int maxSumBST(TreeNode* root) {
int ans = 0;
auto tmp = maxSumBSThelper(root, ans).maxSum;
return ans;
}
};