-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy path0987.cpp
29 lines (29 loc) · 870 Bytes
/
0987.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
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
void dfs(TreeNode *r, int x, int y, map<int, map<int, set<int>>> &m)
{
if (r != nullptr)
{
m[x][y].insert(r->val);
dfs(r->left, x - 1, y + 1, m);
dfs(r->right, x + 1, y + 1, m);
}
}
vector<vector<int>> verticalTraversal(TreeNode *r)
{
map<int, map<int, set<int>>> m;
dfs(r, 0, 0, m);
vector<vector<int>> res;
for (auto itx = m.begin(); itx != m.end(); ++itx)
{
res.push_back(vector<int>());
for (auto ity = itx->second.begin(); ity != itx->second.end(); ++ity)
{
res.back().insert(end(res.back()), begin(ity->second), end(ity->second));
}
}
return res;
}
};