-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
53 lines (46 loc) · 1.58 KB
/
main.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
/**
* 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) {
function<int(TreeNode*, int)> dfs = [&](TreeNode* u, int x) -> int {
if (!u) return x + 1;
int v = x;
v = min(v, dfs(u->left, x - 1));
v = min(v, dfs(u->right, x + 1));
return v;
};
int offset = -dfs(root, 0);
vector<vector<vector<int>>> nodes;
queue<tuple<TreeNode*, int, int>> q;
q.push({root, offset, 0});
while (!q.empty()) {
auto [u, x, y] = q.front();
q.pop();
while (nodes.size() <= x) nodes.push_back({});
while (nodes[x].size() <= y) nodes[x].push_back({});
nodes[x][y].push_back(u->val);
if (u->left) q.push({u->left, x - 1, y + 1});
if (u->right) q.push({u->right, x + 1, y + 1});
}
vector<vector<int>> result;
for (int i = 0; i < nodes.size(); ++i) {
result.push_back({});
for (int j = 0; j < nodes[i].size(); ++j) {
sort(nodes[i][j].begin(), nodes[i][j].end());
for (int v : nodes[i][j]) result.back().push_back(v);
}
}
return result;
}
};