-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleft_view_of_binary_tree.cpp
More file actions
44 lines (42 loc) · 955 Bytes
/
Copy pathleft_view_of_binary_tree.cpp
File metadata and controls
44 lines (42 loc) · 955 Bytes
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
//recursive approach
void leftView_helper(Node *root, int level ,int *maxlevel)
{
if(root == NULL)
return;
if(*maxlevel < level)
{
*maxlevel = level;
cout<<root -> data<<" ";
}
leftView_helper(root -> left , level + 1, maxlevel);
leftView_helper(root -> right , level + 1, maxlevel);
}
void leftView(Node *root)
{
int maxlevel = 0;
leftView_helper(root, 1, &maxlevel);
}
// iterative approach
void leftView(Node *root)
{
if(root == NULL)
return;
int countnode = 0;
queue<Node*>q;
q.push(root);
while(!q.empty())
{
countnode = q.size();
cout<<q.front()->data<<" ";
while(countnode)
{
Node *temp = q.front();
q.pop();
if(temp -> left)
q.push(temp -> left);
if(temp -> right)
q.push(temp -> right);
countnode--;
}
}
}