-
Notifications
You must be signed in to change notification settings - Fork 9
/
Left View Of Binary Tree.cpp
60 lines (47 loc) · 1.51 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
58
59
60
// PROBLEM :- https://practice.geeksforgeeks.org/problems/left-view-of-binary-tree/1#
// GFG | EASY | TREE
// Solution 1 :- T.C. O(N) S.C. O(N)
// Using level order BFS, take the leftmost node from each level.
// Solution 2 :- T.C. O(N) S.C. O(Height)
// First find the max height of tree.
// Build a vector of size Height, representing all the levels. Initialize with -1.
// Recurse in each sub tree, first left, then right.
// If at any level, it's value in array is -1, that means the current node is the leftmost of the current level. Store it.
int heightRecurse(Node *node){
if(node == NULL)
return 0;
int leftHeight = heightRecurse(node->left);
int rightHeight = heightRecurse(node->right);
return 1 + max(leftHeight, rightHeight);
}
void recurseLeftView(Node *node, int curLevel, vector<int> &levelsLeftView){
if(node == NULL)
return;
if(levelsLeftView[curLevel] == -1){
levelsLeftView[curLevel] = node->data;
}
recurseLeftView(node->left, curLevel + 1, levelsLeftView);
recurseLeftView(node->right, curLevel + 1, levelsLeftView);
return;
}
vector<int> leftView(Node *root)
{
if(root == NULL)
return {};
int height = heightRecurse(root);
vector<int> levelsLeftView(height, -1);
recurseLeftView(root, 0, levelsLeftView);
return levelsLeftView;
}
/* A binary tree node
struct Node
{
int data;
struct Node* left;
struct Node* right;
Node(int x){
data = x;
left = right = NULL;
}
};
*/