-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verticalTraversal.cpp
64 lines (54 loc) · 1.85 KB
/
verticalTraversal.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
54
55
56
57
58
59
60
61
62
63
64
/**
* 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 {
void dfs(TreeNode* root, int x, int y, vector<pair<int, pair<int, int>>>& list) {
if(root == NULL) return;
list.push_back(make_pair(root->val, make_pair(x, y)));
dfs(root->left, x - 1, y - 1, list);
dfs(root->right, x + 1, y - 1, list);
}
public:
static bool compare(pair<int, pair<int, int>> L1, pair<int, pair<int, int>> L2) {
// If x is same
if(L1.second.first == L2.second.first) {
// If y is same
if(L1.second.second == L2.second.second) {
return L1.first < L2.first;
} else {
return L1.second.second > L2.second.second;
}
} else {
return L1.second.first < L2.second.first;
}
}
vector<vector<int>> verticalTraversal(TreeNode* root) {
vector<vector<int>> res;
vector<pair<int, pair<int, int>>> list;
map<int, vector<int>> m;
dfs(root, 0, 0, list);
sort(list.begin(), list.end(), compare);
for(pair<int, pair<int, int>> l: list) {
vector<int> line;
if(m.find(l.second.first) != m.end()) {
line = m[l.second.first];
} else {
line = {};
}
line.push_back(l.first);
m[l.second.first] = line;
}
for(auto &x: m) {
res.push_back(x.second);
}
return res;
}
};