-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLC987.cpp
More file actions
104 lines (75 loc) · 2.84 KB
/
LC987.cpp
File metadata and controls
104 lines (75 loc) · 2.84 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
987. Vertical Order Traversal of a Binary Tree
Given the root of a binary tree, calculate the vertical order traversal of the binary tree.
For each node at position (row, col), its left and right children will be at positions (row + 1, col - 1) and (row + 1, col + 1) respectively. The root of the tree is at (0, 0).
The vertical order traversal of a binary tree is a list of top-to-bottom orderings for each column index starting from the leftmost column and ending on the rightmost column. There may be multiple nodes in the same row and same column. In such a case, sort these nodes by their values.
Return the vertical order traversal of the binary tree.
Input: root = [3,9,20,null,null,15,7]
Output: [[9],[3,15],[20],[7]]
Explanation:
Column -1: Only node 9 is in this column.
Column 0: Nodes 3 and 15 are in this column in that order from top to bottom.
Column 1: Only node 20 is in this column.
Column 2: Only node 7 is in this column.
*/
/**
* 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:
Solution() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
vector<vector<int>> verticalTraversal(TreeNode* root) {
queue<pair<TreeNode*, pair<int, int>>> q;
map<int, map<int, multiset<int>>> mpp;
if(root == NULL) return{};
q.push({root, {0, 0}});
while( !q.empty() ){
auto p = q.front();
q.pop();
TreeNode* node = p.first;
int v = p.second.first;
int l = p.second.second;
mpp[v][l].insert(node -> val);
if(node -> left) q.push({node->left, {v - 1, l + 1}});
if(node -> right) q.push({node->right, {v + 1, l + 1}});
}
vector<vector<int>> ans;
for(auto it : mpp){
vector<int> col;
for(auto x : it.second){
col.insert(col.end(), x.second.begin(), x.second.end());
// col.insert(where you want to insert, range)
}
ans.push_back(col);
}
return ans;
}
}
void printVerticalOrder(vector<vector<int>>& res) {
for (auto& col : res) {
for (int val : col) cout << val << " ";
cout << endl;
}
}
int main() {
TreeNode* root = new TreeNode(3);
root->left = new TreeNode(9);
root->right = new TreeNode(20);
root->right->left = new TreeNode(15);
root->right->right = new TreeNode(7);
Solution sol;
vector<vector<int>> result = sol.verticalTraversal(root);
printVerticalOrder(result);
return 0;
}