jayati

Merge two BST 's

Oct 12th, 2023
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 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.  
  22. // Function to Build Tree
  23. Node* buildTree(string str)
  24. {
  25.    // Corner Case
  26.    if(str.length() == 0 || str[0] == 'N')
  27.        return NULL;
  28.  
  29.    // Creating vector of strings from input
  30.    // string after spliting by space
  31.    vector<string> ip;
  32.  
  33.    istringstream iss(str);
  34.    for(string str; iss >> str; )
  35.        ip.push_back(str);
  36.  
  37.    // Create the root of the tree
  38.    Node* root = new Node(stoi(ip[0]));
  39.  
  40.    // Push the root to the queue
  41.    queue<Node*> queue;
  42.    queue.push(root);
  43.  
  44.    // Starting from the second element
  45.    int i = 1;
  46.    while(!queue.empty() && i < ip.size()) {
  47.  
  48.        // Get and remove the front of the queue
  49.        Node* currNode = queue.front();
  50.        queue.pop();
  51.  
  52.        // Get the current node's value from the string
  53.        string currVal = ip[i];
  54.  
  55.        // If the left child is not null
  56.        if(currVal != "N") {
  57.  
  58.            // Create the left child for the current node
  59.            currNode->left = new Node(stoi(currVal));
  60.  
  61.            // Push it to the queue
  62.            queue.push(currNode->left);
  63.        }
  64.  
  65.        // For the right child
  66.        i++;
  67.        if(i >= ip.size())
  68.            break;
  69.        currVal = ip[i];
  70.  
  71.        // If the right child is not null
  72.        if(currVal != "N") {
  73.  
  74.            // Create the right child for the current node
  75.            currNode->right = new Node(stoi(currVal));
  76.  
  77.            // Push it to the queue
  78.            queue.push(currNode->right);
  79.        }
  80.        i++;
  81.    }
  82.  
  83.    return root;
  84. }
  85.  
  86.  
  87.  
  88. // } Driver Code Ends
  89. /*
  90. struct Node {
  91.     int data;
  92.     Node *left;
  93.     Node *right;
  94.  
  95.     Node(int val) {
  96.         data = val;
  97.         left = right = NULL;
  98.     }
  99. };
  100. */
  101. class Solution
  102. {
  103.     public:
  104.     //Function to return a list of integers denoting the node
  105.     //values of both the BST in a sorted order.
  106.         vector <int> verticaltraverse(Node *root)     {
  107.         //Your code here
  108.           // Your Code Here
  109.         if(root==NULL)
  110.         {
  111.             return {};
  112.         }
  113.         vector<int> v;
  114.        map<int,vector<int>> m;
  115.        queue<pair<Node*,int>> q;
  116.        q.push({root,0});
  117.      
  118.        while(!q.empty())
  119.        {
  120.            auto it = q.front();
  121.            q.pop();
  122.             vector<int> temp;
  123.            Node* node = it.first;
  124.            int line = it.second;
  125.            
  126.           m[line].push_back(node->data);
  127.            
  128.            if(node->left!=NULL)
  129.            {
  130.                q.push({node->left,line-1});
  131.            }
  132.             if(node->right!=NULL)
  133.            {
  134.                q.push({node->right,line+1});
  135.            }
  136.        }
  137.        for(auto u:m)
  138.        {
  139.            for(int i=0;i<u.second.size();i++)
  140.            {
  141.                v.push_back(u.second[i]);
  142.            }
  143.        }
  144.        return v;
  145.     }
  146.     vector<int> merge(Node *root1, Node *root2)
  147.     {
  148.        //Your code here
  149.        vector<int> ans1=verticaltraverse(root1);
  150.        vector<int> ans2=verticaltraverse(root2);
  151.        vector<int> finans;
  152.      
  153.        for(int i=0;i<ans1.size();i++)
  154.        {
  155.            finans.push_back(ans1[i]);
  156.        }
  157.        for(int i=0;i<ans2.size();i++)
  158.        {
  159.            finans.push_back(ans2[i]);
  160.        }
  161.        sort(finans.begin(),finans.end());
  162.        return finans;
  163.     }
  164. };
  165.  
  166. //{ Driver Code Starts.
  167. int main() {
  168.  
  169.    int t;
  170.    string tc;
  171.    getline(cin, tc);
  172.    t=stoi(tc);
  173.    while(t--)
  174.    {
  175.         string s;
  176.        getline(cin, s);
  177.        Node* root1 = buildTree(s);
  178.  
  179.        getline(cin, s);
  180.        Node* root2 = buildTree(s);
  181.    
  182.        // getline(cin, s);
  183.        Solution obj;
  184.        vector<int> vec = obj.merge(root1, root2);
  185.        for(int i=0;i<vec.size();i++)
  186.             cout << vec[i] << " ";
  187.         cout << endl;
  188.        ///cout<<"~"<<endl;
  189.    }
  190.    return 0;
  191. }
  192. // } Driver Code Ends
  193.  
Advertisement
Add Comment
Please, Sign In to add comment