-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeft_View_of_Binary_Tree.cpp
57 lines (47 loc) · 1.19 KB
/
Left_View_of_Binary_Tree.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
/*
Problem Statement:
-----------------
Given a Binary Tree, print Left view of it. Left view of a Binary Tree is set of nodes visible when tree is visited from Left side. The task is to complete the function leftView(), which accepts root of the tree as argument.
Left view of following tree is 1 2 4 8.
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Example 1:
---------
Input:
1
/ \
3 2
Output: 1 3
Your Task: You just have to complete the function leftView() that prints the left view. The newline is automatically appended by the driver code.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
*/
// Link --> https://practice.geeksforgeeks.org/problems/left-view-of-binary-tree/1#
// Code:
vector <int> result;
int maxLevel = 0;
void LVUtil(Node *root , int level)
{
if(root == NULL)
return;
if(level >= maxLevel)
{
result.push_back(root->data);
maxLevel++;
}
LVUtil(root->left , level+1);
LVUtil(root->right , level+1);
}
vector<int> leftView(Node *root)
{
result.clear();
int level = 0;
LVUtil(root , level);
maxLevel = 0;
return result;
}