Skip to content

Commit 088e642

Browse files
Create left_view_of_binary_tree.cpp
1 parent 2183111 commit 088e642

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

left_view_of_binary_tree.cpp

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
// } Driver Code Ends
112+
113+
114+
/* A binary tree node
115+
116+
struct Node
117+
{
118+
int data;
119+
struct Node* left;
120+
struct Node* right;
121+
122+
Node(int x){
123+
data = x;
124+
left = right = NULL;
125+
}
126+
};
127+
*/
128+
129+
//Function to return a list containing elements of left view of the binary tree.
130+
vector<int> leftView(Node *root)
131+
{
132+
if(!root) return {};
133+
// Your code here
134+
vector<int> result;
135+
queue<Node* > qu;
136+
137+
qu.push(root);
138+
139+
while(!qu.empty()) {
140+
141+
int n = qu.size();
142+
143+
for(int i = 0; i < n; i++) {
144+
145+
146+
auto current = qu.front();
147+
qu.pop();
148+
149+
if(i == 0) result.push_back(current->data);
150+
151+
if(current->left)
152+
qu.push(current->left);
153+
if(current->right)
154+
qu.push(current->right);
155+
}
156+
}
157+
return result;
158+
}

0 commit comments

Comments
 (0)