jayati

Largest BST

Oct 25th, 2023
905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. //{ Driver Code Starts
  2. //Initial Template for C++
  3.  
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define MAX_HEIGHT 100000
  8.  
  9. // Tree Node
  10. struct Node {
  11.     int data;
  12.     Node* left;
  13.     Node* right;
  14. };
  15.  
  16. // Utility function to create a new Tree Node
  17. Node* newNode(int val) {
  18.     Node* temp = new Node;
  19.     temp->data = val;
  20.     temp->left = NULL;
  21.     temp->right = NULL;
  22.  
  23.     return temp;
  24. }
  25.  
  26.  
  27. // Function to Build Tree
  28. Node* buildTree(string str) {
  29.     // Corner Case
  30.     if (str.length() == 0 || str[0] == 'N') return NULL;
  31.  
  32.     // Creating vector of strings from input
  33.     // string after spliting by space
  34.     vector<string> ip;
  35.  
  36.     istringstream iss(str);
  37.     for (string str; iss >> str;) ip.push_back(str);
  38.  
  39.     // Create the root of the tree
  40.     Node* root = newNode(stoi(ip[0]));
  41.  
  42.     // Push the root to the queue
  43.     queue<Node*> queue;
  44.     queue.push(root);
  45.  
  46.     // Starting from the second element
  47.     int i = 1;
  48.     while (!queue.empty() && i < ip.size()) {
  49.  
  50.         // Get and remove the front of the queue
  51.         Node* currNode = queue.front();
  52.         queue.pop();
  53.  
  54.         // Get the current node's value from the string
  55.         string currVal = ip[i];
  56.  
  57.         // If the left child is not null
  58.         if (currVal != "N") {
  59.  
  60.             // Create the left child for the current node
  61.             currNode->left = newNode(stoi(currVal));
  62.  
  63.             // Push it to the queue
  64.             queue.push(currNode->left);
  65.         }
  66.  
  67.         // For the right child
  68.         i++;
  69.         if (i >= ip.size()) break;
  70.         currVal = ip[i];
  71.  
  72.         // If the right child is not null
  73.         if (currVal != "N") {
  74.  
  75.             // Create the right child for the current node
  76.             currNode->right = newNode(stoi(currVal));
  77.  
  78.             // Push it to the queue
  79.             queue.push(currNode->right);
  80.         }
  81.         i++;
  82.     }
  83.  
  84.     return root;
  85. }
  86.  
  87.  
  88. // } Driver Code Ends
  89. /* Tree node structure  used in the program
  90.  
  91. struct Node {
  92.     int data;
  93.     Node *left;
  94.     Node *right;
  95.  
  96.     Node(int val) {
  97.         data = val;
  98.         left = right = NULL;
  99.     }
  100. };*/
  101.  
  102. class NodeVal{
  103.     public:
  104.     int minVal,maxVal,maxSize;
  105.     NodeVal(int minVal,int maxVal,int maxSize){
  106.         this->minVal = minVal;
  107.         this->maxVal = maxVal;
  108.         this->maxSize=maxSize;
  109.     }
  110. };
  111.  
  112. class Solution{
  113.     NodeVal largestBstHelper(Node * root){
  114.         if(!root){
  115.             return NodeVal(INT_MAX,INT_MIN,0);
  116.         }
  117.         auto LEFT = largestBstHelper(root->left);
  118.         auto RIGHT = largestBstHelper(root->right);
  119.        
  120.         if(LEFT.maxVal < root->data and RIGHT.minVal > root->data){
  121.             return NodeVal( min(root->data,LEFT.minVal),
  122.                             max(root->data,RIGHT.maxVal),
  123.                             LEFT.maxSize+RIGHT.maxSize+1
  124.                           );
  125.         }
  126.         else return NodeVal(INT_MIN,INT_MAX,max(LEFT.maxSize,RIGHT.maxSize));
  127.     }
  128.     public:
  129.     int largestBst(Node *root)
  130.     {
  131.         return largestBstHelper(root).maxSize;
  132.     }
  133. };
  134.  
  135. //{ Driver Code Starts.
  136.  
  137. /* Driver program to test size function*/
  138.  
  139.  
  140.  
  141. int main() {
  142.  
  143.    
  144.     int t;
  145.     scanf("%d ", &t);
  146.     while (t--) {
  147.         string s, ch;
  148.         getline(cin, s);
  149.        
  150.         Node* root = buildTree(s);
  151.         Solution ob;
  152.         cout << ob.largestBst(root) << endl;
  153.     }
  154.     return 0;
  155. }
  156.  
  157. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment