-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
40 lines (37 loc) · 1.09 KB
/
Solution.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
/**
* 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) {}
* };
*/
using pii = pair<int, int>;
class Solution {
public:
map<int, vector<pii>> d;
vector<vector<int>> verticalOrder(TreeNode* root) {
dfs(root, 0, 0);
vector<vector<int>> ans;
for (auto& [_, v] : d) {
sort(v.begin(), v.end(), [&](pii& a, pii& b) {
return a.first < b.first;
});
vector<int> t;
for (auto& x : v) {
t.push_back(x.second);
}
ans.push_back(t);
}
return ans;
}
void dfs(TreeNode* root, int depth, int offset) {
if (!root) return;
d[offset].push_back({depth, root->val});
dfs(root->left, depth + 1, offset - 1);
dfs(root->right, depth + 1, offset + 1);
}
};