-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerticalTraversalOfTree.cpp
39 lines (39 loc) · 1.02 KB
/
VerticalTraversalOfTree.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
#include<bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* left = NULL;
Node* right = NULL;
};
vector<int> verticalOrder(Node *root)
{
map<int,map<int,vector<int>>> MapNodes;
queue<pair<Node*,pair<int,int>>>q;
q.push({root,{0,0}});
while(!q.empty())
{
auto p = q.front();
q.pop();
Node* node = p.first;
int vertical = p.second.first;
int level = p.second.second;
MapNodes[vertical][level].push_back(node->data);
if(node->left)
{
q.push({node->left,{vertical-1,level+1}});
}
if(node->right)
{
q.push({node->right,{vertical+1,level+1}});
}
}
vector<int>FinalOutput;
for(auto p : MapNodes)
{
for(auto q: p.second){
FinalOutput.insert(FinalOutput.end(),q.second.begin(),q.second.end());
}
}
return FinalOutput;
}