-
Notifications
You must be signed in to change notification settings - Fork 359
/
s1.cpp
32 lines (32 loc) · 1.09 KB
/
s1.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
// OJ: https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(H)
class Solution {
int ans = 0;
pair<int, int> postorder(TreeNode *root) {
pair<int, int> r{root->val, root->val};
bool isBST = true;
if (root->left) {
auto left = postorder(root->left);
if (root->val <= left.second) isBST = false;
r.second = max(left.second, root->val);
r.first = min(left.first, root->val);
}
if (root->right) {
auto right = postorder(root->right);
if (root->val >= right.first) isBST = false;
r.second = max(right.second, root->val);
r.first = min(right.first, root->val);
}
root->val += (root->left ? root->left->val : 0) + (root->right ? root->right->val : 0);
if (isBST) ans = max(ans, root->val);
return isBST ? r : pair<int,int>{ INT_MIN, INT_MAX };
}
public:
int maxSumBST(TreeNode* root) {
if (!root) return 0;
postorder(root);
return ans;
}
};