-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path987(a).cpp
37 lines (28 loc) · 887 Bytes
/
987(a).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
void solve(TreeNode * root, map<int,vector<pair<int,int>>>&mp , int hd, int depth){
if(root==NULL){
return ;
}
mp[hd].push_back({depth,root->val});
solve(root->left , mp,hd-1,depth+1);
solve(root->right,mp,hd+1,depth+1);
}
class Solution {
public:
vector<vector<int>> verticalTraversal(TreeNode* root) {
vector<vector<int>>A;
if(root==NULL){
return A;
}
map<int,vector<pair<int,int>>>mp;
solve(root,mp,0,0);
for(auto it=mp.begin();it!=mp.end();it++){
sort(it->second.begin(),it->second.end());
vector<int>B;
for(int i=0;i<it->second.size();i++){
B.push_back(it->second[i].second);
}
A.push_back(B);
}
return A;
}
};