Lecture-073 1 Max BST in Binary Tree - CPP
Lecture-073 1 Max BST in Binary Tree - CPP
h"usingnamespaceclassInfopublicintintboolint
Infothisthisthistruethis0InfointintboolintthisthisthisthisInfo* MaxBST(Node* root, intifNULL
newInforeturnMaxBSTMaxBSTnewInfo1maxminiftrueelsefalseifmaxreturnintmain()NULL
"Enter data to create a tree : "buildBinaryTreeintMaxBST"Max BST : "return0// 50 40 60
70 -1// 55 45 35 65 47 -1</span></span>
2 std;
3 {
4 :
5 maxi;
6 mini;
7 isBST;
8 size;
9 () {
10 ->maxi = INT_MIN;
11 ->mini = INT_MAX;
12 ->isBST = ;
13 ->size = ;
14 }
15 ( min, max, isBST, size) {
16 ->maxi = max;
17 ->mini = min;
18 ->isBST = isBST;
19 ->size = size;
20 }
21 };
22 &answer)</span></span>{
23 (root == ) {
24 Info* temp = ();
25 temp;
26 }
27 Info* left = (root->left, answer);
28 Info* right = (root->right, answer);
29 Info* currNode = ();
30 currNode->size = left->size + right->size + ;
31 currNode->maxi = (root->data, right->maxi);
32 currNode->mini = (root->data, left->mini);
33 (left->isBST && right->isBST && (root->data > left->maxi) && (root->data < right-
>mini)) {
34 currNode->isBST = ;
35 } {
36 currNode->isBST = ;
37 }
38 (currNode->isBST) {
39 answer = (answer, currNode->size);
40 }
41 currNode;
42 }</span>{
43 Node* root = ;
44 cout << ;
45 root = (root);
46 answer = INT_MIN;
47 Info* solve = (root, answer);
48 cout << endl << << answer;
49 ;
50 }