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
|
/**
* 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) {}
* };
*/
typedef pair<int, int> PII;
typedef tuple<int, int, int> TIII;
class Solution {
public:
vector<TIII> rec;
void dfs(PII cur, TreeNode* root){
if(root == nullptr){
return;
}
// 列, 行, 节点值
rec.push_back({cur.second, cur.first, root->val});
dfs({cur.first + 1, cur.second - 1}, root->left);
dfs({cur.first + 1, cur.second + 1}, root->right);
}
vector<vector<int>> verticalTraversal(TreeNode* root) {
dfs({0, 0}, root);
vector< vector<int>> res;
sort(rec.begin(), rec.end());
int pre = INT_MIN, idx = -1;
for(const auto& [col, row, val]: rec){
if(idx == -1 || pre != col){
res.push_back({});
idx += 1;
}
res[idx].push_back(val);
pre = col;
}
return res;
}
};
|