jayati

Check for BST

Sep 1st, 2023
975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define MAX_HEIGHT 100000
  5.  
  6. // Tree Node
  7. struct Node {
  8.     int data;
  9.     Node *left;
  10.     Node *right;
  11.  
  12.     Node(int val) {
  13.         data = val;
  14.         left = right = NULL;
  15.     }
  16. };
  17.  
  18.  
  19.  
  20.  
  21. // } Driver Code Ends
  22. class Solution
  23. {
  24.     public:
  25.     //Function to check whether a Binary Tree is BST or not.
  26.     int maxValue(Node* node)
  27. {
  28.     if (node == NULL) {
  29.         return INT16_MIN;
  30.     }
  31.     int value = node->data;
  32.     int leftMax = maxValue(node->left);
  33.     int rightMax = maxValue(node->right);
  34.  
  35.     return max(value, max(leftMax, rightMax));
  36. }
  37.  
  38. int minValue(Node* node)
  39. {
  40.     if (node == NULL) {
  41.         return INT16_MAX;
  42.     }
  43.     int value = node->data;
  44.     int leftMax = minValue(node->left);
  45.     int rightMax = minValue(node->right);
  46.  
  47.     return min(value, min(leftMax, rightMax));
  48. }
  49.  
  50. bool isBST(Node* node)
  51. {
  52.     if (node == NULL)
  53.         return 1;
  54.  
  55.  
  56.     if (node->left != NULL
  57.         && maxValue(node->left) >= node->data)
  58.         return 0;
  59.  
  60.     if (node->right != NULL
  61.         && minValue(node->right) <= node->data)
  62.         return 0;
  63.  
  64.    
  65.     if (!isBST(node->left) || !isBST(node->right))
  66.         return 0;
  67.  
  68.    
  69.     return 1;
  70. }
  71.    
  72.    
  73. };
  74.  
  75.  
  76.  
  77.  
  78. //{ Driver Code Starts.
  79.  
  80. // Function to Build Tree
  81. Node* buildTree(string str)
  82. {
  83.    // Corner Case
  84.    if(str.length() == 0 || str[0] == 'N')
  85.        return NULL;
  86.  
  87.    // Creating vector of strings from input
  88.    // string after spliting by space
  89.    vector<string> ip;
  90.  
  91.    istringstream iss(str);
  92.    for(string str; iss >> str; )
  93.        ip.push_back(str);
  94.  
  95.    // Create the root of the tree
  96.    Node* root = new Node(stoi(ip[0]));
  97.  
  98.    // Push the root to the queue
  99.    queue<Node*> queue;
  100.    queue.push(root);
  101.  
  102.    // Starting from the second element
  103.    int i = 1;
  104.    while(!queue.empty() && i < ip.size()) {
  105.  
  106.        // Get and remove the front of the queue
  107.        Node* currNode = queue.front();
  108.        queue.pop();
  109.  
  110.        // Get the current node's value from the string
  111.        string currVal = ip[i];
  112.  
  113.        // If the left child is not null
  114.        if(currVal != "N") {
  115.  
  116.            // Create the left child for the current node
  117.            currNode->left = new Node(stoi(currVal));
  118.  
  119.            // Push it to the queue
  120.            queue.push(currNode->left);
  121.        }
  122.  
  123.        // For the right child
  124.        i++;
  125.        if(i >= ip.size())
  126.            break;
  127.        currVal = ip[i];
  128.  
  129.        // If the right child is not null
  130.        if(currVal != "N") {
  131.  
  132.            // Create the right child for the current node
  133.            currNode->right = new Node(stoi(currVal));
  134.  
  135.            // Push it to the queue
  136.            queue.push(currNode->right);
  137.        }
  138.        i++;
  139.    }
  140.  
  141.    return root;
  142. }
  143.  
  144. void inorder(Node *root, vector<int> &v)
  145. {
  146.     if(root==NULL)
  147.         return;
  148.  
  149.     inorder(root->left, v);
  150.     v.push_back(root->data);
  151.     inorder(root->right, v);
  152. }
  153.  
  154. int main() {
  155.  
  156.    int t;
  157.    string tc;
  158.    getline(cin, tc);
  159.    t=stoi(tc);
  160.    while(t--)
  161.    {
  162.     string s;
  163.     getline(cin, s);
  164.     Node* root = buildTree(s);
  165.     Solution ob;
  166.     if(ob.isBST(root))
  167.         cout<<"1\n";
  168.        
  169.     else
  170.         cout<<"0\n";
  171.    }
  172.    return 0;
  173. }
  174.  
  175.  
  176. // } Driver Code Ends
  177. // https://practice.geeksforgeeks.org/problems/check-for-bst/
Advertisement
Add Comment
Please, Sign In to add comment