Skip to content

Commit

Permalink
Added some code
Browse files Browse the repository at this point in the history
  • Loading branch information
codeAbinash committed Sep 28, 2023
1 parent 91a3a4c commit d4ac1fc
Show file tree
Hide file tree
Showing 25 changed files with 721 additions and 41 deletions.
39 changes: 39 additions & 0 deletions leetcode/problems/cpp/average-of-levels-in-binary-tree.cpp
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;
}
};
24 changes: 24 additions & 0 deletions leetcode/problems/cpp/binary-tree-pruning.cpp
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;
}
};
18 changes: 18 additions & 0 deletions leetcode/problems/cpp/build-array-from-permutation.1.cpp
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;
}
};
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;
}
};
25 changes: 25 additions & 0 deletions leetcode/problems/cpp/convert-bst-to-greater-tree.cpp
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;
}
};
17 changes: 10 additions & 7 deletions leetcode/problems/cpp/convert-the-temperature.cpp
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 };
}
};
47 changes: 47 additions & 0 deletions leetcode/problems/cpp/cousins-in-binary-tree-ii.cpp
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 leetcode/problems/cpp/create-binary-tree-from-descriptions.cpp
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];
}
};
21 changes: 21 additions & 0 deletions leetcode/problems/cpp/defanging-an-ip-address.cpp
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;
}
};
36 changes: 36 additions & 0 deletions leetcode/problems/cpp/delete-nodes-and-return-forest.cpp
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;
}
};
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;
}
};
Loading

0 comments on commit d4ac1fc

Please sign in to comment.