Skip to content

Commit 5c34a13

Browse files
authored
Largest BST in a Binary Tree
1 parent 12ee22e commit 5c34a13

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/* Hacker Blocks */
2+
/* Title - Largest BST in a Binary Tree */
3+
/* Created By - Akash Modak */
4+
/* Date - 26/10/2020 */
5+
6+
// Given a Binary Tree, write a program that returns the size of the largest subtree which is also a Binary Search Tree (BST)
7+
8+
// Input Format
9+
// 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.
10+
11+
// Constraints
12+
// 2 ≤ N ≤ 10^3
13+
14+
// Output Format
15+
// A single integer denoting the size ( no of nodes in tree ) of largest BST in the given tree.
16+
17+
// Sample Input
18+
// 4
19+
// 60 65 50 70
20+
// 50 65 60 70
21+
// Sample Output
22+
// 2
23+
// Explanation
24+
// The tree looks like
25+
26+
// 60
27+
// / \
28+
// 65 70
29+
// /
30+
// 50
31+
// The largest BST subtree is
32+
33+
// 65
34+
// /
35+
// 50
36+
// which has the size 2 i.e. it has 2 nodes in it.
37+
38+
#include<bits/stdc++.h>
39+
using namespace std;
40+
class node{
41+
public:
42+
int data;
43+
node* left,*right;
44+
node(int d){
45+
data = d;
46+
left=right=NULL;
47+
}
48+
};
49+
class NB{
50+
public:
51+
int numOfNode,maxNode;
52+
bool BST;
53+
};
54+
node * build(int in[],int pre[],int start,int end,int &preIndex){
55+
if(start>end){
56+
return NULL;
57+
}
58+
node * root = new node(pre[preIndex]);
59+
int i;
60+
for(i=start;i<=end;i++){
61+
if(in[i]==pre[preIndex])
62+
break;
63+
}
64+
preIndex++;
65+
root->left = build(in,pre,start,i-1,preIndex);
66+
root->right = build(in,pre,i+1,end,preIndex);
67+
return root;
68+
}
69+
bool checkBST(node* root,int minV=INT_MIN,int maxV = INT_MAX){
70+
if(root==NULL)
71+
return true;
72+
73+
if(root->data>=minV && root->data<=maxV && checkBST(root->left,minV,root->data) && checkBST(root->right,root->data,maxV))
74+
return true;
75+
return false;
76+
}
77+
int num(node* root){
78+
79+
if(root==NULL)
80+
return 0;
81+
int l = num(root->left);
82+
int r = num(root->right);
83+
return l+r+1;
84+
}
85+
NB longestBST(node* root){
86+
NB temp;
87+
if(root==NULL){
88+
temp.numOfNode = 0;
89+
temp.BST = true;
90+
temp.maxNode = 0;
91+
return temp;
92+
}
93+
94+
NB left = longestBST(root->left);
95+
NB right = longestBST(root->right);
96+
97+
if(left.BST and right.BST and checkBST(root)){
98+
temp.BST = true;
99+
temp.numOfNode = num(root);
100+
// temp.maxNode = max(temp.numOfNode,max(left.maxNode,right.maxNode));
101+
// cout<<left.numOfNode<<" "<<right.numOfNode<<" "<<num(root)<<" "<<temp.maxNode<<endl;
102+
}
103+
else{
104+
temp.BST =false;
105+
temp.numOfNode = 0;
106+
// temp.maxNode = 0;
107+
}
108+
temp.maxNode = max(temp.numOfNode,max(left.maxNode,right.maxNode));
109+
return temp;
110+
}
111+
int main() {
112+
int n;
113+
cin>>n;
114+
int pre[n],in[n];
115+
for(int i=0;i<n;i++)
116+
cin>>pre[i];
117+
for(int i=0;i<n;i++)
118+
cin>>in[i];
119+
int p = 0;
120+
node* root = build(in,pre,0,n-1,p);
121+
NB temp = longestBST(root);
122+
// inorder(root);cout<<endl;
123+
// cout<<num(root)<<endl;
124+
cout<<temp.maxNode<<endl;
125+
return 0;
126+
}

0 commit comments

Comments
 (0)