jayati

Bottom View of Binary Tree

Oct 12th, 2023
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 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. {
  9.     int data;
  10.     Node* left;
  11.     Node* right;
  12. };
  13.  
  14. // Utility function to create a new Tree Node
  15. Node* newNode(int val)
  16. {
  17.     Node* temp = new Node;
  18.     temp->data = val;
  19.     temp->left = NULL;
  20.     temp->right = NULL;
  21.  
  22.     return temp;
  23. }
  24.  
  25.  
  26. vector <int> bottomView(Node *root);
  27.  
  28. // Function to Build Tree
  29. Node* buildTree(string str)
  30. {
  31.     // Corner Case
  32.     if(str.length() == 0 || str[0] == 'N')
  33.         return NULL;
  34.  
  35.     // Creating vector of strings from input
  36.     // string after spliting by space
  37.     vector<string> ip;
  38.  
  39.     istringstream iss(str);
  40.     for(string str; iss >> str; )
  41.         ip.push_back(str);
  42.  
  43.     // Create the root of the tree
  44.     Node* root = newNode(stoi(ip[0]));
  45.  
  46.     // Push the root to the queue
  47.     queue<Node*> queue;
  48.     queue.push(root);
  49.  
  50.     // Starting from the second element
  51.     int i = 1;
  52.     while(!queue.empty() && i < ip.size()) {
  53.  
  54.         // Get and remove the front of the queue
  55.         Node* currNode = queue.front();
  56.         queue.pop();
  57.  
  58.         // Get the current node's value from the string
  59.         string currVal = ip[i];
  60.  
  61.         // If the left child is not null
  62.         if(currVal != "N") {
  63.  
  64.             // Create the left child for the current node
  65.             currNode->left = newNode(stoi(currVal));
  66.  
  67.             // Push it to the queue
  68.             queue.push(currNode->left);
  69.         }
  70.  
  71.         // For the right child
  72.         i++;
  73.         if(i >= ip.size())
  74.             break;
  75.         currVal = ip[i];
  76.  
  77.         // If the right child is not null
  78.         if(currVal != "N") {
  79.  
  80.             // Create the right child for the current node
  81.             currNode->right = newNode(stoi(currVal));
  82.  
  83.             // Push it to the queue
  84.             queue.push(currNode->right);
  85.         }
  86.         i++;
  87.     }
  88.  
  89.     return root;
  90. }
  91.  
  92.  
  93. // } Driver Code Ends
  94. //Function to return a list containing the bottom view of the given tree.
  95.  
  96. class Solution {
  97.   public:
  98.  
  99.     vector <int> bottomView(Node *root) {
  100.         // Your Code Here
  101.         if(root==NULL)
  102.         {
  103.             return {};
  104.         }
  105.         vector<int> v;
  106.        map<int,int> m;
  107.        queue<pair<Node*,int>> q;
  108.        q.push({root,0});
  109.        
  110.        while(!q.empty())
  111.        {
  112.            auto it = q.front();
  113.            q.pop();
  114.            Node* node = it.first;
  115.            int line = it.second;
  116.            m[line] = node->data;
  117.            
  118.            if(node->left!=NULL)
  119.            {
  120.                q.push({node->left,line-1});
  121.            }
  122.             if(node->right!=NULL)
  123.            {
  124.                q.push({node->right,line+1});
  125.            }
  126.        }
  127.        for(auto u:m)
  128.        {
  129.            v.push_back(u.second);
  130.        }
  131.        return v;
  132.        
  133.     }
  134. };
  135.  
  136. //{ Driver Code Starts.
  137.  
  138. int main() {
  139.     int t;
  140.     string tc;
  141.     getline(cin, tc);
  142.     t=stoi(tc);
  143.     while(t--)
  144.     {
  145.         string s ,ch;
  146.         getline(cin, s);
  147.         Node* root = buildTree(s);
  148.         Solution ob;
  149.         vector <int> res = ob.bottomView(root);
  150.         for (int i : res) cout << i << " ";
  151.         cout << endl;
  152.     }
  153.     return 0;
  154. }
  155.  
  156.  
  157.  
  158. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment