-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsoln-1.cpp
40 lines (39 loc) · 1.25 KB
/
soln-1.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
struct Record {
long mn;
long mx;
int nnodes;
bool isBST;
Record(long mn, long mx, int nnodes, bool isBST): mn(mn), mx(mx), nnodes(nnodes), isBST(isBST) {}
};
class Solution {
public:
int largestBSTSubtree(TreeNode* root) {
int ans = 0;
Postorder(root, & ans);
return ans;
}
private:
Record Postorder(TreeNode * root, int * ans) {
if (root == nullptr) return Record(LONG_MAX, LONG_MIN, 0, true);
Record left_record = Postorder(root->left, ans);
Record right_record = Postorder(root->right, ans);
long mn = min((long) root->val, min(left_record.mn, right_record.mn));
long mx = max((long) root->val, max(left_record.mx, right_record.mx));
int nnodes = 1 + left_record.nnodes + right_record.nnodes;
bool isBST = false;
if (left_record.isBST && right_record.isBST && root->val > left_record.mx && root->val < right_record.mn) {
isBST = true;
*ans = max(*ans, nnodes);
}
return Record(mn, mx, nnodes, isBST);
}
};