jayati

Left View of Binary Tree

Oct 11th, 2023
1,356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. // Tree Node
  6. struct Node
  7. {
  8.     int data;
  9.     Node* left;
  10.     Node* right;
  11. };
  12.  
  13. vector<int> leftView(struct Node *root);
  14.  
  15. // Utility function to create a new Tree Node
  16. Node* newNode(int val)
  17. {
  18.     Node* temp = new Node;
  19.     temp->data = val;
  20.     temp->left = NULL;
  21.     temp->right = NULL;
  22.  
  23.     return temp;
  24. }
  25.  
  26.  
  27. // Function to Build Tree
  28. Node* buildTree(string str)
  29. {
  30.     // Corner Case
  31.     if(str.length() == 0 || str[0] == 'N')
  32.         return NULL;
  33.  
  34.     // Creating vector of strings from input
  35.     // string after spliting by space
  36.     vector<string> ip;
  37.  
  38.     istringstream iss(str);
  39.     for(string str; iss >> str; )
  40.         ip.push_back(str);
  41.  
  42.     // for(string i:ip)
  43.     //     cout<<i<<" ";
  44.     // cout<<endl;
  45.     // Create the root of the tree
  46.     Node* root = newNode(stoi(ip[0]));
  47.  
  48.     // Push the root to the queue
  49.     queue<Node*> queue;
  50.     queue.push(root);
  51.  
  52.     // Starting from the second element
  53.     int i = 1;
  54.     while(!queue.empty() && i < ip.size()) {
  55.  
  56.         // Get and remove the front of the queue
  57.         Node* currNode = queue.front();
  58.         queue.pop();
  59.  
  60.         // Get the current node's value from the string
  61.         string currVal = ip[i];
  62.  
  63.         // If the left child is not null
  64.         if(currVal != "N") {
  65.  
  66.             // Create the left child for the current node
  67.             currNode->left = newNode(stoi(currVal));
  68.  
  69.             // Push it to the queue
  70.             queue.push(currNode->left);
  71.         }
  72.  
  73.         // For the right child
  74.         i++;
  75.         if(i >= ip.size())
  76.             break;
  77.         currVal = ip[i];
  78.  
  79.         // If the right child is not null
  80.         if(currVal != "N") {
  81.  
  82.             // Create the right child for the current node
  83.             currNode->right = newNode(stoi(currVal));
  84.  
  85.             // Push it to the queue
  86.             queue.push(currNode->right);
  87.         }
  88.         i++;
  89.     }
  90.  
  91.     return root;
  92. }
  93.  
  94.  
  95. int main() {
  96.     int t;
  97.     scanf("%d ",&t);
  98.     while(t--)
  99.     {
  100.         string s;
  101.         getline(cin,s);
  102.         Node* root = buildTree(s);
  103.         vector<int> vec = leftView(root);
  104.         for(int x : vec)
  105.         cout<<x<<" ";
  106.         cout << endl;
  107.     }
  108.     return 0;
  109. }
  110.  
  111.  
  112. // } Driver Code Ends
  113.  
  114.  
  115. /* A binary tree node
  116.  
  117. struct Node
  118. {
  119.     int data;
  120.     struct Node* left;
  121.     struct Node* right;
  122.    
  123.     Node(int x){
  124.         data = x;
  125.         left = right = NULL;
  126.     }
  127. };
  128.  */
  129.  
  130. //Function to return a list containing elements of left view of the binary tree.
  131.  
  132. void solve(Node* root, vector<int>&v,int lev){
  133.     if(!root){
  134.         return;
  135.     }
  136.     if(lev == v.size()){
  137.         v.push_back(root->data);
  138.     }
  139.     // Go to the next level of the tree i.e.
  140.     solve(root->left, v, lev+1);//left subtree traversal
  141.     solve(root->right, v, lev+1);//Right subtree traversal
  142. }
  143. vector<int> leftView(Node *root)
  144. {
  145.     vector<int>v;
  146.     solve(root, v, 0);
  147.     return v;
  148.    
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment