-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC7.CPP
More file actions
37 lines (37 loc) · 1.12 KB
/
LC7.CPP
File metadata and controls
37 lines (37 loc) · 1.12 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
// Proble link : https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3415/
#include <bits/stdc++.h>
using namespace std;
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:
map<int, set<pair<int,int>>> m;
void pre(TreeNode* root, int horiz, int vert)
{
if (!root)
return;
m[horiz].insert({vert, root->val});
pre(root->left, horiz-1, vert+1);
pre(root->right, horiz+1, vert+1);
}
vector<vector<int>> verticalTraversal(TreeNode* root) {
vector<vector<int>> ans;
pre(root,0,0);
for (auto it1:m)
{
vector<int> c;
for (auto it2:it1.second)
{
c.push_back(it2.second);
}
ans.push_back(c);
}
return ans;
}
};