-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathLargestBSTInATree.cpp
73 lines (54 loc) · 1.57 KB
/
LargestBSTInATree.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
// https://www.codingninjas.com/codestudio/problems/largest-bst-subtree_893103?leftPanelTab=1&utm_source=youtube&utm_medium=affiliate&utm_campaign=Lovebabbar
#include <bits/stdc++.h>
/************************************************************
Following is the Binary Tree node structure
template <typename T>
class TreeNode {
public :
T data;
TreeNode<T> *left;
TreeNode<T> *right;
TreeNode(T data) {
this -> data = data;
left = NULL;
right = NULL;
}
~TreeNode() {
if(left)
delete left;
if(right)
delete right;
}
};
************************************************************/
class info{
public:
int max;
int min;
bool isBST;
int size;
};
info solve(TreeNode<int>* root, int &ans){
if(root==NULL)
return {INT_MIN, INT_MAX, true, 0};
info left= solve(root->left, ans);
info right= solve(root->right, ans);
info currNode;
currNode.size= left.size+right.size+1;
currNode.max= max(root->data, right.max);
currNode.min= min(root->data, left.min);
if(left.isBST && right.isBST && (root->data>left.max && root->data<right.min))
currNode.isBST= true;
else
currNode.isBST= false;
if(currNode.isBST)
ans= max(ans, currNode.size);
return currNode;
}
int largestBST(TreeNode<int>* root)
{
// Write your code here.
int maxSize= 0;
solve(root, maxSize);
return maxSize;
}