-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverticalTraversal.cpp
More file actions
40 lines (40 loc) · 1.26 KB
/
verticalTraversal.cpp
File metadata and controls
40 lines (40 loc) · 1.26 KB
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
/**
* 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:
vector<vector<int>> verticalTraversal(TreeNode* root) {
map<int, vector<int> > m;
queue<pair<TreeNode*, int> > q;
q.push({root, 0});
while (!q.empty()) {
int len = q.size();
unordered_map<int, multiset<int> > temp;
while (len--) {
auto [node, col] = q.front();
q.pop();
temp[col].insert(node->val);
if (node->left) q.push({node->left, col - 1});
if (node->right) q.push({node->right, col + 1});
}
for (auto it: temp) {
auto &[e, ms] = it;
m[e].reserve(m[e].size() + ms.size());
m[e].insert(m[e].end(), ms.begin(), ms.end());
}
}
vector<vector<int> > ans;
for (auto it: m) {
ans.push_back(it.second);
}
return ans;
}
};