forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(GeeksForGeeks)_Vertical_Traversal_of_Binary_Tree.cpp
82 lines (68 loc) · 1.51 KB
/
(GeeksForGeeks)_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<bits/stdc++.h>
using namespace std;
#define ln "\n";
#define TC() int t; cin >> t; while(t--)
#define IO() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
typedef long long ll;
struct node{
int val;
node *left = nullptr, *right = nullptr;
};
void preOrder(node* root){
if(root == nullptr) return;
cout << root->val << " ";
preOrder(root->left);
preOrder(root->right);
}
vector<int> verticalOrder(node *root){
map<int,vector<int>> m;
queue<pair<node*,int>> q;
q.push({root,0});
while(!q.empty()){
node* n = q.front().first;
int x = q.front().second;
m[x].push_back(n->val);
q.pop();
if(n->left != nullptr) q.push({n->left,x-1});
if(n->right != nullptr) q.push({n->right,x+1});
}
vector<int> ans;
for(auto i : m){
for(auto j : i.second) ans.push_back(j);
}
return ans;
}
int main(){
IO();
// TC() solve();
node *root = new node;
node *node1 = new node;
node *node2 = new node;
node *node3 = new node;
node *node4 = new node;
node *node5 = new node;
node *node6 = new node;
node *node7 = new node;
node *node8 = new node;
root->val = 1;
node1->val = 2;
node2->val = 3;
root->left = node1;
root->right = node2;
node3->val = 4;
node4->val = 5;
node1->left = node3;
node1->right = node4;
node5->val = 6;
node6->val = 7;
node2->left = node5;
node2->right = node6;
node7->val = 8;
node8->val = 9;
node5->right = node7;
node6->right = node8;
// preOrder(root);
vector<int> v = verticalOrder(root);
for(auto i : v) cout << i <<" ";
return 0;
}