-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91a3a4c
commit d4ac1fc
Showing
25 changed files
with
721 additions
and
41 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
leetcode/problems/cpp/average-of-levels-in-binary-tree.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(n) | ||
|
||
#include "vector" | ||
#include "queue" | ||
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: | ||
vector<double> averageOfLevels(TreeNode* root) { | ||
vector<double> ans; | ||
if (!root) return ans; | ||
queue<TreeNode*> q; | ||
q.push(root); | ||
while (!q.empty()) { | ||
int n = q.size(); | ||
double sum = 0; | ||
for (int i = 0; i < n; i++) { | ||
TreeNode* node = q.front(); | ||
q.pop(); | ||
sum += node->val; | ||
if (node->left) q.push(node->left); | ||
if (node->right) q.push(node->right); | ||
} | ||
ans.push_back(sum / n); | ||
} | ||
return ans; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(n) | ||
|
||
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: | ||
TreeNode* pruneTree(TreeNode* root) { | ||
if (!root) return root; | ||
root->left = pruneTree(root->left); | ||
root->right = pruneTree(root->right); | ||
if (root->left || root->right) return root; | ||
if (root->val == 0) return nullptr; | ||
return root; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(n) | ||
|
||
#include "vector" | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
vector<int> buildArray(vector<int>& nums) { | ||
int len = nums.size(); | ||
vector<int> ans(len); | ||
for (int i = 0;i < len; i++) { | ||
ans[i] = nums[nums[i]]; | ||
} | ||
return ans; | ||
} | ||
}; |
61 changes: 61 additions & 0 deletions
61
leetcode/problems/cpp/construct-binary-tree-from-preorder-and-postorder-traversal.CPP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(n) | ||
|
||
#include "vector" | ||
#include "stack" | ||
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) {} | ||
}; | ||
|
||
|
||
// Recursuve | ||
class Solution { | ||
int preIndex, postIndex; | ||
TreeNode* construct(vector<int>& pre, vector<int>& post) { | ||
TreeNode* root = new TreeNode(pre[preIndex++]); | ||
if (root->val != post[postIndex]) | ||
root->left = construct(pre, post); | ||
if (root->val != post[postIndex]) | ||
root->right = construct(pre, post); | ||
postIndex++; | ||
return root; | ||
} | ||
public: | ||
TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder) { | ||
preIndex = postIndex = 0; | ||
return construct(preorder, postorder); | ||
} | ||
}; | ||
|
||
|
||
// Iterative | ||
class Solution { | ||
public: | ||
TreeNode* constructFromPrePost(vector<int>& pre, vector<int>& post) { | ||
stack<TreeNode*> s; | ||
TreeNode* root = new TreeNode(pre[0]); | ||
s.push(root); | ||
for (int i = 1, j = 0; i < pre.size(); ++i) { | ||
TreeNode* node = new TreeNode(pre[i]); | ||
while (s.top()->val == post[j]) { | ||
s.pop(); | ||
++j; | ||
} | ||
if (s.top()->left == nullptr) | ||
s.top()->left = node; | ||
else | ||
s.top()->right = node; | ||
s.push(node); | ||
} | ||
return root; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(n) | ||
|
||
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 { | ||
int sum = 0; | ||
public: | ||
TreeNode* convertBST(TreeNode* root) { | ||
if (root == nullptr) return root; | ||
convertBST(root->right); | ||
sum += root->val; | ||
root->val = sum; | ||
convertBST(root->left); | ||
return root; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
// by @codeAbinash | ||
// Time Complexity : O(1) | ||
// Space Complexity : O(1) | ||
// Time : O(1) | ||
// Space : O(1) | ||
|
||
#include "vector" | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
vector<double> convertTemperature(double celsius) { | ||
return { celsius + 273.15, celsius * 1.80 + 32.00 }; | ||
} | ||
}; | ||
public: | ||
vector<double> convertTemperature(double celsius) { | ||
return { celsius + 273.15, celsius * 1.80 + 32.00 }; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(n) | ||
|
||
#include "queue" | ||
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: | ||
TreeNode* replaceValueInTree(TreeNode* root) { | ||
queue<TreeNode*> q; q.push(root); | ||
root->val = 0; | ||
while (!q.empty()) { | ||
int n = q.size(), sum = 0; | ||
vector<TreeNode*> buffer; | ||
while (n--) { | ||
TreeNode* node = q.front(); q.pop(); | ||
buffer.push_back(node); | ||
if (node->left) { | ||
q.push(node->left); | ||
sum += node->left->val; | ||
} | ||
if (node->right) { | ||
q.push(node->right); | ||
sum += node->right->val; | ||
} | ||
} | ||
for (auto node : buffer) { | ||
int t = sum; | ||
if (node->left) t -= node->left->val; | ||
if (node->right) t -= node->right->val; | ||
if (node->left) node->left->val = t; | ||
if (node->right) node->right->val = t; | ||
} | ||
} | ||
return root; | ||
} | ||
}; |
40 changes: 40 additions & 0 deletions
40
leetcode/problems/cpp/create-binary-tree-from-descriptions.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(n) | ||
|
||
#include "vector" | ||
#include "unordered_map" | ||
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: | ||
TreeNode* createBinaryTree(vector<vector<int>>& desc) { | ||
unordered_map<int, TreeNode*> mp; | ||
int xr = 0; | ||
for (auto& it : desc) { | ||
if (mp.find(it[0]) == mp.end()) { | ||
mp[it[0]] = new TreeNode(it[0]); | ||
xr ^= it[0]; | ||
} | ||
if (mp.find(it[1]) == mp.end()) { | ||
mp[it[1]] = new TreeNode(it[1]); | ||
xr ^= it[1]; | ||
} | ||
} | ||
for (auto& it : desc) { | ||
if (it[2] == 1) mp[it[0]]->left = mp[it[1]]; | ||
else mp[it[0]]->right = mp[it[1]]; | ||
xr ^= it[1]; | ||
} | ||
return mp[xr]; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(n) | ||
|
||
#include "string" | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
string defangIPaddr(string address) { | ||
string ans; | ||
int len = address.size(); | ||
for (int i = 0; i < len; i++) { | ||
if (address[i] == '.') | ||
ans.push_back('['), ans.push_back('.'), ans.push_back(']'); | ||
else | ||
ans.push_back(address[i]); | ||
} | ||
return ans; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(n) | ||
|
||
#include "vector" | ||
#include "unordered_set" | ||
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 { | ||
vector<TreeNode*> ans; | ||
unordered_set<int> to_delete_set; | ||
public: | ||
vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) { | ||
for (int i : to_delete) to_delete_set.insert(i); | ||
dfs(root, true); | ||
return ans; | ||
} | ||
TreeNode* dfs(TreeNode* root, bool is_root) { | ||
if (!root) return nullptr; | ||
bool deleted = to_delete_set.find(root->val) != to_delete_set.end(); | ||
if (is_root && !deleted) ans.push_back(root); | ||
root->left = dfs(root->left, deleted); | ||
root->right = dfs(root->right, deleted); | ||
return deleted ? nullptr : root; | ||
} | ||
}; |
27 changes: 14 additions & 13 deletions
27
leetcode/problems/cpp/final-value-of-variable-after-performing-operations.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
// by @codeAbinash | ||
// Time Complexity : O(n) | ||
// Space Complexity : O(1) | ||
// Time : O(n) | ||
// Space : O(1) | ||
|
||
#include "vector" | ||
#include "string" | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int finalValueAfterOperations(vector<string>& operations) { | ||
int value = 0; | ||
for(auto &str : operations){ | ||
if(str == "++X" || str == "X++") | ||
value++; | ||
else | ||
value--; | ||
} | ||
return value; | ||
} | ||
}; | ||
int finalValueAfterOperations(vector<string>& operations) { | ||
int result = 0; | ||
for (auto& op : operations) { | ||
result += (op[1] == '+') ? 1 : -1; | ||
} | ||
return result; | ||
} | ||
}; |
Oops, something went wrong.