Largest BST GFG POTD 22 - 7 - 2024 SOLVED

preview_player
Показать описание
Code in comments.
Рекомендации по теме
Комментарии
Автор

class Solution{
public:
/*You are required to complete this method */
// Return the size of the largest sub-tree which is also a BST
int nodes(Node * root){
if(root==NULL)return 0;
return 1 + nodes(root->left)+ nodes(root->right);
}
bool bst(Node * root, int x, int y){
if(root==NULL)return true;
if(x<root->data && y>root->data){
bool a = bst( root->left, x, root->data);
bool b = bst(root->right, root->data, y);
return a&b;
}
else return false;
}
int largestBst(Node *root)
{
if(bst(root, INT_MIN, INT_MAX)) return nodes(root);
return max(largestBst(root->left), largestBst(root->right));
}
};

myKodingacademia