Skip to content

Commit 1d0b647

Browse files
✨ Day 7; Week 1 ✔️
1 parent 5845879 commit 1d0b647

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
4. [Power of Four](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3412/) ➡️ [CPP Solution](Week1/powerOfFour.cpp)
1212
5. [Add and Search Word - Data structure design](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3413/) ➡️ [CPP Solution](Week1/wordDictionary.cpp)
1313
6. [Find All Duplicates in an Array](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3414/) ➡️ [CPP Solution](Week1/findDuplicates.cpp)
14+
7. [Vertical Order Traversal of a Binary Tree](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3415/) ➡️ [CPP Solution](Week1/verticalTraversal.cpp)
1415

1516
## Where to find me? 🌟
1617
* [Website](https://akashwho.codes/)

Week1/verticalTraversal.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
void dfs(TreeNode* root, int x, int y, vector<pair<int, pair<int, int>>>& list) {
14+
if(root == NULL) return;
15+
16+
list.push_back(make_pair(root->val, make_pair(x, y)));
17+
18+
dfs(root->left, x - 1, y - 1, list);
19+
dfs(root->right, x + 1, y - 1, list);
20+
}
21+
public:
22+
static bool compare(pair<int, pair<int, int>> L1, pair<int, pair<int, int>> L2) {
23+
// If x is same
24+
if(L1.second.first == L2.second.first) {
25+
// If y is same
26+
if(L1.second.second == L2.second.second) {
27+
return L1.first < L2.first;
28+
} else {
29+
return L1.second.second > L2.second.second;
30+
}
31+
} else {
32+
return L1.second.first < L2.second.first;
33+
}
34+
}
35+
36+
vector<vector<int>> verticalTraversal(TreeNode* root) {
37+
vector<vector<int>> res;
38+
vector<pair<int, pair<int, int>>> list;
39+
map<int, vector<int>> m;
40+
41+
dfs(root, 0, 0, list);
42+
43+
sort(list.begin(), list.end(), compare);
44+
45+
for(pair<int, pair<int, int>> l: list) {
46+
vector<int> line;
47+
48+
if(m.find(l.second.first) != m.end()) {
49+
line = m[l.second.first];
50+
} else {
51+
line = {};
52+
}
53+
54+
line.push_back(l.first);
55+
m[l.second.first] = line;
56+
}
57+
58+
for(auto &x: m) {
59+
res.push_back(x.second);
60+
}
61+
62+
return res;
63+
}
64+
};

0 commit comments

Comments
 (0)