-
Notifications
You must be signed in to change notification settings - Fork 0
/
333.cpp
72 lines (59 loc) · 1.39 KB
/
333.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool dfs(TreeNode* t, int& min, int& max, int& cnt)
{
bool bl, br;
int mil = 1000000, mir = 1000000, mal = -1000000, mar = -1000000, cntl = 0, cntr = 0;
if(t->left)
{
bl = dfs(t->left, mil, mal, cntl);
}
else
{
bl = true;
}
if(t->right)
{
br = dfs(t->right, mir, mar, cntr);
}
else
{
br = true;
}
max = t->val;
if(mal > max)max = mal;
if(mar > max)max = mar;
min = t->val;
if(mil < min)min = mil;
if(mir < min)min = mir;
cnt = cntl + cntr + 1;
cout << t->val << "|" << bl <<"|" << br << "|" << mal << "|" << mir << endl;
if(bl && br && t->val > mal && t->val < mir)
{
if(cnt > maxCnt)maxCnt = cnt;
return true;
}
else
{
return false;
}
}
int largestBSTSubtree(TreeNode* root) {
cout << "fuck" << endl;
if(!root)return 0;
maxCnt = 0;
int max, min, cnt;
dfs(root, min, max, cnt);
return maxCnt;
}
int maxCnt;
};