-
Notifications
You must be signed in to change notification settings - Fork 0
/
333largetBstSubtree.cpp
111 lines (101 loc) · 2.51 KB
/
333largetBstSubtree.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
//
//Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it.
//
//Note:
// A subtree must include all of its descendants.
// Here's an example:
//
// 10
// / \
// 5 15
// / \ \
// 1 8 7
//
// The Largest BST Subtree in this case is the highlighted one.
// The return value is the subtree's size, which is 3.
//
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
int checkBST(TreeNode *root, int min, int max)
{
if (root == NULL)
{
return 0;
}
if ((root->val < min) || (root->val > max))
{
return -1;
}
int left = checkBST(root->left, min, root->val);
int right = checkBST(root->right, root->val, max);
if (left == -1 || right == -1)
{
return -1;
}
return 1 + left + right;
}
void maxsub_BST_helper(TreeNode *root, int &res)
{
if (root == NULL)
{
return;
}
int ret = checkBST(root, INT_MIN, INT_MAX);
if ( ret != -1)
{
if(ret > res)
{
res = ret;
}
return;
}
maxsub_BST_helper(root->left, res);
maxsub_BST_helper(root->right, res);
}
int max_calc(int a, int b)
{
return (a>b)?a:b;
}
int maxsub_BST2_helper(TreeNode *root, int &min, int &max, int &res)
{
if (root == NULL)
{
return 1;
}
int left_min = INT_MIN, right_min = INT_MIN, left_max = INT_MAX, right_max = INT_MAX;
int res_left = 0, res_right = 0;
int left = maxsub_BST2_helper(root->left, left_min, left_max, res_left);
int right = maxsub_BST2_helper(root->right, right_min, right_max, res_right);
if (left && right)
{
if (((!root->left) || (root->left && left_max < root->val)) && ((!root->right) || (root->right && right_min > root->val)))
{
res = res_left + res_right + 1;
min = (root->left) ? left_min : root->val;
max = (root->right) ? right_max : root->val;
return 1;
}
}
res = max_calc(res_left, res_right);
return 0;
}
class Solution {
public:
int largestBSTSubtree(TreeNode* root)
{
//int res = 0;
//maxsub_BST_helper(root, res);
//return res;
int min = INT_MIN, max = INT_MAX, res = 0;
maxsub_BST2_helper(root, min, max, res);
return res;
}
};