jayati

Lowest Common Ancestor in a BST

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