|
| 1 | +#include <bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +// Tree Node |
| 5 | +struct Node |
| 6 | +{ |
| 7 | + int data; |
| 8 | + Node* left; |
| 9 | + Node* right; |
| 10 | +}; |
| 11 | + |
| 12 | +vector<int> leftView(struct Node *root); |
| 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 | +// Function to Build Tree |
| 27 | +Node* buildTree(string str) |
| 28 | +{ |
| 29 | + // Corner Case |
| 30 | + if(str.length() == 0 || str[0] == 'N') |
| 31 | + return NULL; |
| 32 | + |
| 33 | + // Creating vector of strings from input |
| 34 | + // string after spliting by space |
| 35 | + vector<string> ip; |
| 36 | + |
| 37 | + istringstream iss(str); |
| 38 | + for(string str; iss >> str; ) |
| 39 | + ip.push_back(str); |
| 40 | + |
| 41 | + // for(string i:ip) |
| 42 | + // cout<<i<<" "; |
| 43 | + // cout<<endl; |
| 44 | + // Create the root of the tree |
| 45 | + Node* root = newNode(stoi(ip[0])); |
| 46 | + |
| 47 | + // Push the root to the queue |
| 48 | + queue<Node*> queue; |
| 49 | + queue.push(root); |
| 50 | + |
| 51 | + // Starting from the second element |
| 52 | + int i = 1; |
| 53 | + while(!queue.empty() && i < ip.size()) { |
| 54 | + |
| 55 | + // Get and remove the front of the queue |
| 56 | + Node* currNode = queue.front(); |
| 57 | + queue.pop(); |
| 58 | + |
| 59 | + // Get the current node's value from the string |
| 60 | + string currVal = ip[i]; |
| 61 | + |
| 62 | + // If the left child is not null |
| 63 | + if(currVal != "N") { |
| 64 | + |
| 65 | + // Create the left child for the current node |
| 66 | + currNode->left = newNode(stoi(currVal)); |
| 67 | + |
| 68 | + // Push it to the queue |
| 69 | + queue.push(currNode->left); |
| 70 | + } |
| 71 | + |
| 72 | + // For the right child |
| 73 | + i++; |
| 74 | + if(i >= ip.size()) |
| 75 | + break; |
| 76 | + currVal = ip[i]; |
| 77 | + |
| 78 | + // If the right child is not null |
| 79 | + if(currVal != "N") { |
| 80 | + |
| 81 | + // Create the right child for the current node |
| 82 | + currNode->right = newNode(stoi(currVal)); |
| 83 | + |
| 84 | + // Push it to the queue |
| 85 | + queue.push(currNode->right); |
| 86 | + } |
| 87 | + i++; |
| 88 | + } |
| 89 | + |
| 90 | + return root; |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +int main() { |
| 95 | + int t; |
| 96 | + scanf("%d ",&t); |
| 97 | + while(t--) |
| 98 | + { |
| 99 | + string s; |
| 100 | + getline(cin,s); |
| 101 | + Node* root = buildTree(s); |
| 102 | + vector<int> vec = leftView(root); |
| 103 | + for(int x : vec) |
| 104 | + cout<<x<<" "; |
| 105 | + cout << endl; |
| 106 | + } |
| 107 | + return 0; |
| 108 | +} |
| 109 | + |
| 110 | +// } Driver Code Ends |
| 111 | + |
| 112 | + |
| 113 | +/* A binary tree node |
| 114 | +
|
| 115 | +struct Node |
| 116 | +{ |
| 117 | + int data; |
| 118 | + struct Node* left; |
| 119 | + struct Node* right; |
| 120 | + |
| 121 | + Node(int x){ |
| 122 | + data = x; |
| 123 | + left = right = NULL; |
| 124 | + } |
| 125 | +}; |
| 126 | + */ |
| 127 | + |
| 128 | +// A wrapper over leftViewUtil() |
| 129 | +vector<int> leftView(Node *root) |
| 130 | +{ |
| 131 | + // Your code here |
| 132 | + vector<int>v; |
| 133 | + if(root==NULL) |
| 134 | + return v; |
| 135 | + queue <Node *> q; |
| 136 | + q.push(root); |
| 137 | + while(!q.empty()) |
| 138 | + { |
| 139 | + int size=q.size(); |
| 140 | + for(int i=0;i<size;i++) |
| 141 | + { |
| 142 | + Node *cur=q.front(); |
| 143 | + if(i==0) |
| 144 | + v.push_back(cur->data); |
| 145 | + q.pop(); |
| 146 | + if(cur->left) |
| 147 | + q.push(cur->left); |
| 148 | + if(cur->right) |
| 149 | + q.push(cur->right); |
| 150 | + } |
| 151 | + } |
| 152 | + return v; |
| 153 | +} |
0 commit comments