forked from Amisha-here/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind_BST_In_Binary_Tree.cpp
More file actions
151 lines (151 loc) · 2.4 KB
/
Find_BST_In_Binary_Tree.cpp
File metadata and controls
151 lines (151 loc) · 2.4 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//8
//70 65 50 66 67 68 80 72
//50 65 66 67 68 80 70 72
//if(T==NULL)
// return 0;
// if(T->data<Min || T->data>Max)
// {
// M=max(M,Find(T,INT_MAX));
// return 0;
// }
// if(T->rChild==NULL && T->lChild==NULL)
// return 1;
// long long int X=Find(T->lChild,T->data);
// long long int Y=Find(T->rChild,Max);
// if(X!=0 && Y!=0)
// {
// if(T->lChild->data<T->data && T->rChild->data>T->data)
// {
// M=max(M,X+Y+1);
// return max(X,Y)+1;
// }
// else if(T->lChild->data<T->data)
// {
// M=max(M,X+1);
// return X+1;
// }
// else if(T->rChild->data>T->data)
// {
// M=max(M,Y+1);
// return Y+1;
// }
// else
// {
// return 1;
// }
// }
// else if(X==0 && Y!=0)
// {
// if(T->rChild->data>T->data)
// {
// M=max(M,Y+1);
// return Y+1;
// }
// return 1;
// }
// else if(X!=0 && Y==0)
// {
// if(T->lChild->data<T->data)
// {
// M=max(M,X+1);
// return X+1;
// }
// return 1;
// }
// else
// {
// return 1;
// }
#include<iostream>
#include<queue>
#include<stack>
#include<climits>
#include<algorithm>
#include<cmath>
using namespace std;
class Node{
public:
long long int data;
Node *lChild,*rChild;
}*Root=NULL;
long long int M=0;
Node* CreateNode(long long int Val)
{
Node *A=new Node;
A->data=Val;
A->lChild=A->rChild=NULL;
return A;
}
Node * Create(long long int A[],long long int B[],long long int S,long long int E)
{
static long long int i=0;
long long int j,Mid;
if(S>=E)
return NULL;
long long int X=A[i];
Node *T=CreateNode(X);
for(j=S;j<E;j++)
{
if(B[j]==X)
{
Mid=j;
}
}
++i;
T->lChild=Create(A,B,S,Mid);
T->rChild=Create(A,B,Mid+1,E);
return T;
}
int isBST(Node *T,int Max,int Min)
{
if(T==NULL)
return true;
if(T->data>Min && T->data<Max)
{
bool X=isBST(T->lChild,T->data,Min);
bool Y=isBST(T->rChild,Max,T->data);
if(X==true && Y==true)
return true;
}
return false;
}
int size(Node *T)
{
if(T==NULL)
return 0;
return 1+size(T->lChild)+size(T->rChild);
}
int LargestBST(Node *T)
{
if(T==NULL)
return 0;
if(isBST(T,INT_MAX,INT_MIN))
{
return size(T);
}
return max(LargestBST(T->lChild),LargestBST(T->rChild));
}
void PreOrder(Node *R)
{
if(R==NULL)
return;
cout<<R->data<<" ";
PreOrder(R->lChild);
PreOrder(R->rChild);
}
int main()
{
long long int N,i;
cin>>N;
long long int A[N],B[N];
for(i=0;i<N;i++)
{
cin>>A[i];
}
for(i=0;i<N;i++)
{
cin>>B[i];
}
Root=Create(A,B,0,N);
cout<<LargestBST(Root);
}