-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLargest BST in a Binary Tree.cpp
126 lines (114 loc) · 2.89 KB
/
Largest BST in a Binary Tree.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* Hacker Blocks */
/* Title - Largest BST in a Binary Tree */
/* Created By - Akash Modak */
/* Date - 26/10/2020 */
// Given a Binary Tree, write a program that returns the size of the largest subtree which is also a Binary Search Tree (BST)
// Input Format
// The first line of input will contain an integer n. The next line will contain n integers denoting the the preorder traversal of the BT. The next line will contain n more integers denoting the inorder traversal of the BT.
// Constraints
// 2 ≤ N ≤ 10^3
// Output Format
// A single integer denoting the size ( no of nodes in tree ) of largest BST in the given tree.
// Sample Input
// 4
// 60 65 50 70
// 50 65 60 70
// Sample Output
// 2
// Explanation
// The tree looks like
// 60
// / \
// 65 70
// /
// 50
// The largest BST subtree is
// 65
// /
// 50
// which has the size 2 i.e. it has 2 nodes in it.
#include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node* left,*right;
node(int d){
data = d;
left=right=NULL;
}
};
class NB{
public:
int numOfNode,maxNode;
bool BST;
};
node * build(int in[],int pre[],int start,int end,int &preIndex){
if(start>end){
return NULL;
}
node * root = new node(pre[preIndex]);
int i;
for(i=start;i<=end;i++){
if(in[i]==pre[preIndex])
break;
}
preIndex++;
root->left = build(in,pre,start,i-1,preIndex);
root->right = build(in,pre,i+1,end,preIndex);
return root;
}
bool checkBST(node* root,int minV=INT_MIN,int maxV = INT_MAX){
if(root==NULL)
return true;
if(root->data>=minV && root->data<=maxV && checkBST(root->left,minV,root->data) && checkBST(root->right,root->data,maxV))
return true;
return false;
}
int num(node* root){
if(root==NULL)
return 0;
int l = num(root->left);
int r = num(root->right);
return l+r+1;
}
NB longestBST(node* root){
NB temp;
if(root==NULL){
temp.numOfNode = 0;
temp.BST = true;
temp.maxNode = 0;
return temp;
}
NB left = longestBST(root->left);
NB right = longestBST(root->right);
if(left.BST and right.BST and checkBST(root)){
temp.BST = true;
temp.numOfNode = num(root);
// temp.maxNode = max(temp.numOfNode,max(left.maxNode,right.maxNode));
// cout<<left.numOfNode<<" "<<right.numOfNode<<" "<<num(root)<<" "<<temp.maxNode<<endl;
}
else{
temp.BST =false;
temp.numOfNode = 0;
// temp.maxNode = 0;
}
temp.maxNode = max(temp.numOfNode,max(left.maxNode,right.maxNode));
return temp;
}
int main() {
int n;
cin>>n;
int pre[n],in[n];
for(int i=0;i<n;i++)
cin>>pre[i];
for(int i=0;i<n;i++)
cin>>in[i];
int p = 0;
node* root = build(in,pre,0,n-1,p);
NB temp = longestBST(root);
// inorder(root);cout<<endl;
// cout<<num(root)<<endl;
cout<<temp.maxNode<<endl;
return 0;
}