forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vertical Order Traversal of a Binary Tree.cpp
56 lines (52 loc) · 1.71 KB
/
Vertical Order Traversal of a 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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
// hd - horizontal distance
// vertical order traversal starts from least hd to highest hd
// on moving left hd decreases by 1, on moving right it increases by 1
// should do level order traversal to get the nodes with same hd in correct order
vector<vector<int>> verticalTraversal(TreeNode* root) {
map<int,vector<int>> mp;
queue<pair<TreeNode*,int>> q;
q.push({root,0});
while(!q.empty()){
int sz = q.size();
map<int,multiset<int>> temp;
for(int i=0;i<sz;i++){
auto pr = q.front();
q.pop();
temp[pr.second].insert(pr.first->val);
if(pr.first->left != NULL){
q.push({pr.first->left,pr.second-1});
}
if(pr.first->right != NULL){
q.push({pr.first->right,pr.second+1});
}
}
for(auto pr:temp){
for(auto val:pr.second){
mp[pr.first].push_back(val);
}
}
}
vector<vector<int>> ans;
for(auto pr:mp){
vector<int> temp;
for(auto val:pr.second){
temp.push_back(val);
}
ans.push_back(temp);
}
return ans;
}
};