Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- #include <bits/stdc++.h>
- using namespace std;
- #define MAX_HEIGHT 100000
- // Tree Node
- struct Node {
- int data;
- Node *left;
- Node *right;
- Node(int val) {
- data = val;
- left = right = NULL;
- }
- };
- // Function to Build Tree
- Node* buildTree(string str)
- {
- // Corner Case
- if(str.length() == 0 || str[0] == 'N')
- return NULL;
- // Creating vector of strings from input
- // string after spliting by space
- vector<string> ip;
- istringstream iss(str);
- for(string str; iss >> str; )
- ip.push_back(str);
- // Create the root of the tree
- Node* root = new Node(stoi(ip[0]));
- // Push the root to the queue
- queue<Node*> queue;
- queue.push(root);
- // Starting from the second element
- int i = 1;
- while(!queue.empty() && i < ip.size()) {
- // Get and remove the front of the queue
- Node* currNode = queue.front();
- queue.pop();
- // Get the current node's value from the string
- string currVal = ip[i];
- // If the left child is not null
- if(currVal != "N") {
- // Create the left child for the current node
- currNode->left = new Node(stoi(currVal));
- // Push it to the queue
- queue.push(currNode->left);
- }
- // For the right child
- i++;
- if(i >= ip.size())
- break;
- currVal = ip[i];
- // If the right child is not null
- if(currVal != "N") {
- // Create the right child for the current node
- currNode->right = new Node(stoi(currVal));
- // Push it to the queue
- queue.push(currNode->right);
- }
- i++;
- }
- return root;
- }
- // } Driver Code Ends
- /*
- struct Node {
- int data;
- Node *left;
- Node *right;
- Node(int val) {
- data = val;
- left = right = NULL;
- }
- };
- */
- class Solution
- {
- public:
- //Function to return a list of integers denoting the node
- //values of both the BST in a sorted order.
- vector <int> verticaltraverse(Node *root) {
- //Your code here
- // Your Code Here
- if(root==NULL)
- {
- return {};
- }
- vector<int> v;
- map<int,vector<int>> m;
- queue<pair<Node*,int>> q;
- q.push({root,0});
- while(!q.empty())
- {
- auto it = q.front();
- q.pop();
- vector<int> temp;
- Node* node = it.first;
- int line = it.second;
- m[line].push_back(node->data);
- if(node->left!=NULL)
- {
- q.push({node->left,line-1});
- }
- if(node->right!=NULL)
- {
- q.push({node->right,line+1});
- }
- }
- for(auto u:m)
- {
- for(int i=0;i<u.second.size();i++)
- {
- v.push_back(u.second[i]);
- }
- }
- return v;
- }
- vector<int> merge(Node *root1, Node *root2)
- {
- //Your code here
- vector<int> ans1=verticaltraverse(root1);
- vector<int> ans2=verticaltraverse(root2);
- vector<int> finans;
- for(int i=0;i<ans1.size();i++)
- {
- finans.push_back(ans1[i]);
- }
- for(int i=0;i<ans2.size();i++)
- {
- finans.push_back(ans2[i]);
- }
- sort(finans.begin(),finans.end());
- return finans;
- }
- };
- //{ Driver Code Starts.
- int main() {
- int t;
- string tc;
- getline(cin, tc);
- t=stoi(tc);
- while(t--)
- {
- string s;
- getline(cin, s);
- Node* root1 = buildTree(s);
- getline(cin, s);
- Node* root2 = buildTree(s);
- // getline(cin, s);
- Solution obj;
- vector<int> vec = obj.merge(root1, root2);
- for(int i=0;i<vec.size();i++)
- cout << vec[i] << " ";
- cout << endl;
- ///cout<<"~"<<endl;
- }
- return 0;
- }
- // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment