-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathvertical_traversal_of_Binary_tree.cpp
45 lines (38 loc) · 1.42 KB
/
vertical_traversal_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
// problem link: https://practice.geeksforgeeks.org/problems/print-a-binary-tree-in-vertical-order/1
// use a map and store horizontal distance from root as keys and elements as value(array)
// idea is similar to that of vertical sum of binary tree
// the difference here is that we have to do level order traversal
// learnt about how to traverse through vector of maps
void levelOrder(Node* root, map<int, vector<int>> &mp)
{
queue<pair<Node *, int>> q; // node as key and horizontal dist from root as value
q.push({root, 0});
while(q.size() != 0)
{
int count = q.size();
for(int i=0; i<count; i++)
{
auto top = q.front();
Node *curr = top.first;
int horizontalDist = top.second;
mp[horizontalDist].push_back(curr->data);
q.pop();
if(curr -> left) q.push({curr->left, horizontalDist - 1});
if(curr -> right) q.push({curr->right, horizontalDist + 1});
}
}
}
vector<int> verticalOrder(Node *root)
{
map<int, vector<int>> mp;
levelOrder(root, mp);
vector<int> ans;
for(auto i: mp)
{
for(auto j : i.second)
{
ans.push_back(j);
}
}
return ans;
}