jayati

Ceil in BST

Oct 24th, 2023
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. struct Node {
  6.     int data;
  7.     struct Node* left;
  8.     struct Node* right;
  9. };
  10. // Utility function to create a new Tree Node
  11. Node* newNode(int val) {
  12.     Node* temp = new Node;
  13.     temp->data = val;
  14.     temp->left = NULL;
  15.     temp->right = NULL;
  16.  
  17.     return temp;
  18. }
  19. // Function to Build Tree
  20. Node* buildTree(string str) {
  21.     // Corner Case
  22.     if (str.length() == 0 || str[0] == 'N') return NULL;
  23.  
  24.     // Creating vector of strings from input
  25.     // string after spliting by space
  26.     vector<string> ip;
  27.  
  28.     istringstream iss(str);
  29.     for (string str; iss >> str;) ip.push_back(str);
  30.  
  31.     // Create the root of the tree
  32.     Node* root = newNode(stoi(ip[0]));
  33.  
  34.     // Push the root to the queue
  35.     queue<Node*> queue;
  36.     queue.push(root);
  37.  
  38.     // Starting from the second element
  39.     int i = 1;
  40.     while (!queue.empty() && i < ip.size()) {
  41.  
  42.         // Get and remove the front of the queue
  43.         Node* currNode = queue.front();
  44.         queue.pop();
  45.  
  46.         // Get the current node's value from the string
  47.         string currVal = ip[i];
  48.  
  49.         // If the left child is not null
  50.         if (currVal != "N") {
  51.  
  52.             // Create the left child for the current node
  53.             currNode->left = newNode(stoi(currVal));
  54.  
  55.             // Push it to the queue
  56.             queue.push(currNode->left);
  57.         }
  58.  
  59.         // For the right child
  60.         i++;
  61.         if (i >= ip.size()) break;
  62.         currVal = ip[i];
  63.  
  64.         // If the right child is not null
  65.         if (currVal != "N") {
  66.  
  67.             // Create the right child for the current node
  68.             currNode->right = newNode(stoi(currVal));
  69.  
  70.             // Push it to the queue
  71.             queue.push(currNode->right);
  72.         }
  73.         i++;
  74.     }
  75.  
  76.     return root;
  77. }
  78.  
  79. int findCeil(Node* root, int input);
  80.  
  81. int main() {
  82.  
  83.     int t;
  84.     scanf("%d ", &t);
  85.     while (t--) {
  86.         string s;
  87.         getline(cin >> ws, s);
  88.         int x;
  89.         cin >> x;
  90.         Node* root = buildTree(s);
  91.         cout << findCeil(root, x) << endl;
  92.     }
  93.     return 1;
  94. }
  95.  
  96. // } Driver Code Ends
  97.  
  98.  
  99. // User function Template for C++
  100.  
  101. // Function to return the ceil of given number in BST.
  102. int nxgreat=INT_MAX;
  103. int findCeil(Node* root, int input) {
  104.    int ans=-1;  
  105.     while(root!=NULL)
  106.     {
  107.     if(root->data>=input)
  108.      {
  109.          ans=root->data;
  110.          root = root->left;
  111.      }
  112.      else
  113.      {
  114.          root = root->right;
  115.      }
  116.     }
  117.     return ans;
  118.      
  119.    
  120.    
  121.     // Your code here
  122. }
Advertisement
Add Comment
Please, Sign In to add comment