forked from ravya1108/hactoberfest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary tree left view.cpp
70 lines (69 loc) · 1.1 KB
/
Binary tree left view.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<queue>
#include<math.h>
using namespace std;
class node {
public:
int data;
node* left;
node* right;
node(int d)
{
data = d;
left = NULL;
right = NULL;
}
};
node* buildtreeLevel()
{
queue<node*>q;
int d;
cin >> d;
node* root = new node(d);
q.push(root);
int c1, c2;
while (!q.empty())
{
node* f = q.front();
q.pop();
cin >> c1 >> c2;
if (c1 != -1)
{
f->left = new node(c1);
q.push(f->left);
}
if (c2 != -1)
{
f->right = new node(c2);
q.push(f->right);
}
}
return root;
}
void leftView(node *root){
if(root==NULL){
return;
}
queue<node*>q;
q.push(root);
while(!q.empty()){
int n=q.size();
for(int i=1;i<=n;i++){
node* temp=q.front();
q.pop();
if(i==1){
cout<<temp->data<<" ";
}
if(temp->left!=NULL){
q.push(temp->left);
}
if(temp->right!=NULL){
q.push(temp->right);
}
}
}
}
int main(){
node* root = buildtreeLevel();
leftView(root);
}