From d4ac1fcf02f5d38eae09780b207a0f77d560462c Mon Sep 17 00:00:00 2001 From: Abinash Karmakar Date: Thu, 28 Sep 2023 22:34:34 +0530 Subject: [PATCH] Added some code --- .../cpp/average-of-levels-in-binary-tree.cpp | 39 ++++++++++++ leetcode/problems/cpp/binary-tree-pruning.cpp | 24 ++++++++ .../cpp/build-array-from-permutation.1.cpp | 18 ++++++ ...-from-preorder-and-postorder-traversal.CPP | 61 +++++++++++++++++++ .../cpp/convert-bst-to-greater-tree.cpp | 25 ++++++++ .../problems/cpp/convert-the-temperature.cpp | 17 +++--- .../cpp/cousins-in-binary-tree-ii.cpp | 47 ++++++++++++++ .../create-binary-tree-from-descriptions.cpp | 40 ++++++++++++ .../problems/cpp/defanging-an-ip-address.cpp | 21 +++++++ .../cpp/delete-nodes-and-return-forest.cpp | 36 +++++++++++ ...f-variable-after-performing-operations.cpp | 27 ++++---- .../cpp/find-bottom-left-tree-value.cpp | 31 ++++++++++ .../cpp/flip-equivalent-binary-trees.cpp | 22 +++++++ leetcode/problems/cpp/jewels-and-stones.cpp | 30 +++++---- .../cpp/kth-smallest-element-in-a-bst.cpp | 35 +++++++++++ .../longest-zigzag-path-in-a-binary-tree.cpp | 33 ++++++++++ .../problems/cpp/maximum-binary-tree-ii.cpp | 23 +++++++ .../cpp/most-frequent-subtree-sum.cpp | 36 +++++++++++ ...-minimum-number-of-deci-binary-numbers.cpp | 23 ++++--- ...udo-palindromic-paths-in-a-binary-tree.cpp | 30 +++++++++ ...recover-a-tree-from-preorder-traversal.cpp | 36 +++++++++++ leetcode/problems/cpp/spiral-matrix-iv.cpp | 31 ++++++++++ .../sum-of-root-to-leaf-binary-numbers.cpp | 25 ++++++++ .../cpp/trim-a-binary-search-tree.cpp | 27 ++++++++ .../problems/cpp/univalued-binary-tree.cpp | 25 ++++++++ 25 files changed, 721 insertions(+), 41 deletions(-) create mode 100644 leetcode/problems/cpp/average-of-levels-in-binary-tree.cpp create mode 100644 leetcode/problems/cpp/binary-tree-pruning.cpp create mode 100644 leetcode/problems/cpp/build-array-from-permutation.1.cpp create mode 100644 leetcode/problems/cpp/construct-binary-tree-from-preorder-and-postorder-traversal.CPP create mode 100644 leetcode/problems/cpp/convert-bst-to-greater-tree.cpp create mode 100644 leetcode/problems/cpp/cousins-in-binary-tree-ii.cpp create mode 100644 leetcode/problems/cpp/create-binary-tree-from-descriptions.cpp create mode 100644 leetcode/problems/cpp/defanging-an-ip-address.cpp create mode 100644 leetcode/problems/cpp/delete-nodes-and-return-forest.cpp create mode 100644 leetcode/problems/cpp/find-bottom-left-tree-value.cpp create mode 100644 leetcode/problems/cpp/flip-equivalent-binary-trees.cpp create mode 100644 leetcode/problems/cpp/kth-smallest-element-in-a-bst.cpp create mode 100644 leetcode/problems/cpp/longest-zigzag-path-in-a-binary-tree.cpp create mode 100644 leetcode/problems/cpp/maximum-binary-tree-ii.cpp create mode 100644 leetcode/problems/cpp/most-frequent-subtree-sum.cpp create mode 100644 leetcode/problems/cpp/pseudo-palindromic-paths-in-a-binary-tree.cpp create mode 100644 leetcode/problems/cpp/recover-a-tree-from-preorder-traversal.cpp create mode 100644 leetcode/problems/cpp/spiral-matrix-iv.cpp create mode 100644 leetcode/problems/cpp/sum-of-root-to-leaf-binary-numbers.cpp create mode 100644 leetcode/problems/cpp/trim-a-binary-search-tree.cpp create mode 100644 leetcode/problems/cpp/univalued-binary-tree.cpp diff --git a/leetcode/problems/cpp/average-of-levels-in-binary-tree.cpp b/leetcode/problems/cpp/average-of-levels-in-binary-tree.cpp new file mode 100644 index 0000000..e59f455 --- /dev/null +++ b/leetcode/problems/cpp/average-of-levels-in-binary-tree.cpp @@ -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 averageOfLevels(TreeNode* root) { + vector ans; + if (!root) return ans; + queue 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; + } +}; diff --git a/leetcode/problems/cpp/binary-tree-pruning.cpp b/leetcode/problems/cpp/binary-tree-pruning.cpp new file mode 100644 index 0000000..3a1291e --- /dev/null +++ b/leetcode/problems/cpp/binary-tree-pruning.cpp @@ -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; + } +}; diff --git a/leetcode/problems/cpp/build-array-from-permutation.1.cpp b/leetcode/problems/cpp/build-array-from-permutation.1.cpp new file mode 100644 index 0000000..26831a2 --- /dev/null +++ b/leetcode/problems/cpp/build-array-from-permutation.1.cpp @@ -0,0 +1,18 @@ +// by @codeAbinash +// Time : O(n) +// Space : O(n) + +#include "vector" +using namespace std; + +class Solution { +public: + vector buildArray(vector& nums) { + int len = nums.size(); + vector ans(len); + for (int i = 0;i < len; i++) { + ans[i] = nums[nums[i]]; + } + return ans; + } +}; diff --git a/leetcode/problems/cpp/construct-binary-tree-from-preorder-and-postorder-traversal.CPP b/leetcode/problems/cpp/construct-binary-tree-from-preorder-and-postorder-traversal.CPP new file mode 100644 index 0000000..967c322 --- /dev/null +++ b/leetcode/problems/cpp/construct-binary-tree-from-preorder-and-postorder-traversal.CPP @@ -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& pre, vector& 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& preorder, vector& postorder) { + preIndex = postIndex = 0; + return construct(preorder, postorder); + } +}; + + +// Iterative +class Solution { +public: + TreeNode* constructFromPrePost(vector& pre, vector& post) { + stack 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; + } +}; diff --git a/leetcode/problems/cpp/convert-bst-to-greater-tree.cpp b/leetcode/problems/cpp/convert-bst-to-greater-tree.cpp new file mode 100644 index 0000000..c84e0fa --- /dev/null +++ b/leetcode/problems/cpp/convert-bst-to-greater-tree.cpp @@ -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; + } +}; diff --git a/leetcode/problems/cpp/convert-the-temperature.cpp b/leetcode/problems/cpp/convert-the-temperature.cpp index 7a31c50..e66d27e 100644 --- a/leetcode/problems/cpp/convert-the-temperature.cpp +++ b/leetcode/problems/cpp/convert-the-temperature.cpp @@ -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 convertTemperature(double celsius) { - return { celsius + 273.15, celsius * 1.80 + 32.00 }; - } -}; \ No newline at end of file +public: + vector convertTemperature(double celsius) { + return { celsius + 273.15, celsius * 1.80 + 32.00 }; + } +}; diff --git a/leetcode/problems/cpp/cousins-in-binary-tree-ii.cpp b/leetcode/problems/cpp/cousins-in-binary-tree-ii.cpp new file mode 100644 index 0000000..7a5d912 --- /dev/null +++ b/leetcode/problems/cpp/cousins-in-binary-tree-ii.cpp @@ -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 q; q.push(root); + root->val = 0; + while (!q.empty()) { + int n = q.size(), sum = 0; + vector 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; + } +}; diff --git a/leetcode/problems/cpp/create-binary-tree-from-descriptions.cpp b/leetcode/problems/cpp/create-binary-tree-from-descriptions.cpp new file mode 100644 index 0000000..e2818a7 --- /dev/null +++ b/leetcode/problems/cpp/create-binary-tree-from-descriptions.cpp @@ -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>& desc) { + unordered_map 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]; + } +}; diff --git a/leetcode/problems/cpp/defanging-an-ip-address.cpp b/leetcode/problems/cpp/defanging-an-ip-address.cpp new file mode 100644 index 0000000..41edaa0 --- /dev/null +++ b/leetcode/problems/cpp/defanging-an-ip-address.cpp @@ -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; + } +}; diff --git a/leetcode/problems/cpp/delete-nodes-and-return-forest.cpp b/leetcode/problems/cpp/delete-nodes-and-return-forest.cpp new file mode 100644 index 0000000..05433be --- /dev/null +++ b/leetcode/problems/cpp/delete-nodes-and-return-forest.cpp @@ -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 ans; + unordered_set to_delete_set; +public: + vector delNodes(TreeNode* root, vector& 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; + } +}; diff --git a/leetcode/problems/cpp/final-value-of-variable-after-performing-operations.cpp b/leetcode/problems/cpp/final-value-of-variable-after-performing-operations.cpp index 1c0747b..066cf21 100644 --- a/leetcode/problems/cpp/final-value-of-variable-after-performing-operations.cpp +++ b/leetcode/problems/cpp/final-value-of-variable-after-performing-operations.cpp @@ -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& operations) { - int value = 0; - for(auto &str : operations){ - if(str == "++X" || str == "X++") - value++; - else - value--; - } - return value; - } -}; \ No newline at end of file + int finalValueAfterOperations(vector& operations) { + int result = 0; + for (auto& op : operations) { + result += (op[1] == '+') ? 1 : -1; + } + return result; + } +}; diff --git a/leetcode/problems/cpp/find-bottom-left-tree-value.cpp b/leetcode/problems/cpp/find-bottom-left-tree-value.cpp new file mode 100644 index 0000000..cd2935c --- /dev/null +++ b/leetcode/problems/cpp/find-bottom-left-tree-value.cpp @@ -0,0 +1,31 @@ +// 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 depth = 0, num; +public: + int findBottomLeftValue(TreeNode* root) { + if (!root) return 0; + dfs(root, 1); + return num; + } + void dfs(TreeNode* root, int d) { + if (!root) return; + if (d > depth) { + depth = d; + num = root->val; + } + dfs(root->left, d + 1); + dfs(root->right, d + 1); + } +}; diff --git a/leetcode/problems/cpp/flip-equivalent-binary-trees.cpp b/leetcode/problems/cpp/flip-equivalent-binary-trees.cpp new file mode 100644 index 0000000..0aa3d8d --- /dev/null +++ b/leetcode/problems/cpp/flip-equivalent-binary-trees.cpp @@ -0,0 +1,22 @@ +// 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: + bool flipEquiv(TreeNode* root1, TreeNode* root2) { + if (!root1 || !root2) return !root1 && !root2; + if (root1->val != root2->val) return false; + return (flipEquiv(root1->left, root2->left) && flipEquiv(root1->right, root2->right)) || + (flipEquiv(root1->left, root2->right) && flipEquiv(root1->right, root2->left)); + } +}; diff --git a/leetcode/problems/cpp/jewels-and-stones.cpp b/leetcode/problems/cpp/jewels-and-stones.cpp index 6ba2259..3f7928a 100644 --- a/leetcode/problems/cpp/jewels-and-stones.cpp +++ b/leetcode/problems/cpp/jewels-and-stones.cpp @@ -1,15 +1,21 @@ // by @codeAbinash -// Time Complexity : O(n) -// Space Complexity : O(1) +// Time : O(n) +// Space : O(n) + +#include "string" +#include "unordered_set" +using namespace std; class Solution { - public: - int numJewelsInStones(string jewels, string stones) { - bool j[128] = {0}; - for (auto &c : jewels) j[c] = true; - int count = 0; - for (auto &c : stones) - if (j[c]) count++; - return count; - } -}; \ No newline at end of file +public: + int numJewelsInStones(string jewels, string stones) { + int count = 0; + unordered_set s; + for (auto ch : jewels) + s.insert(ch); + for (auto ch : stones) + if (s.find(ch) != s.end()) + count++; + return count; + } +}; diff --git a/leetcode/problems/cpp/kth-smallest-element-in-a-bst.cpp b/leetcode/problems/cpp/kth-smallest-element-in-a-bst.cpp new file mode 100644 index 0000000..78e9919 --- /dev/null +++ b/leetcode/problems/cpp/kth-smallest-element-in-a-bst.cpp @@ -0,0 +1,35 @@ +// 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 n, ans = 0; + void dfs(TreeNode* root) { + if (!root || n == 0) return; + dfs(root->left); + if (n == 1) { + ans = root->val; + n--; + return; + } else { + n--; + } + dfs(root->right); + } +public: + + int kthSmallest(TreeNode* root, int k) { + n = k; + dfs(root); + return ans; + } +}; diff --git a/leetcode/problems/cpp/longest-zigzag-path-in-a-binary-tree.cpp b/leetcode/problems/cpp/longest-zigzag-path-in-a-binary-tree.cpp new file mode 100644 index 0000000..fd478e4 --- /dev/null +++ b/leetcode/problems/cpp/longest-zigzag-path-in-a-binary-tree.cpp @@ -0,0 +1,33 @@ +// 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 maxStep = 0; +public: + int longestZigZag(TreeNode* root) { + dfs(root, true, 0); + dfs(root, false, 0); + return maxStep; + } + void dfs(TreeNode* root, bool isLeft, int step) { + if (!root) return; + maxStep = max(maxStep, step); + if (isLeft) { + dfs(root->left, false, step + 1); + dfs(root->right, true, 1); + } else { + dfs(root->right, true, step + 1); + dfs(root->left, false, 1); + } + } +}; diff --git a/leetcode/problems/cpp/maximum-binary-tree-ii.cpp b/leetcode/problems/cpp/maximum-binary-tree-ii.cpp new file mode 100644 index 0000000..28a42d0 --- /dev/null +++ b/leetcode/problems/cpp/maximum-binary-tree-ii.cpp @@ -0,0 +1,23 @@ +// 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* insertIntoMaxTree(TreeNode* root, int val) { + if (!root) return new TreeNode(val); + if (val > root->val) + return new TreeNode(val, root, nullptr); + root->right = insertIntoMaxTree(root->right, val); + return root; + } +}; diff --git a/leetcode/problems/cpp/most-frequent-subtree-sum.cpp b/leetcode/problems/cpp/most-frequent-subtree-sum.cpp new file mode 100644 index 0000000..2392672 --- /dev/null +++ b/leetcode/problems/cpp/most-frequent-subtree-sum.cpp @@ -0,0 +1,36 @@ +// 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 { + unordered_map freq; + int maxFreq = 0; +public: + vector findFrequentTreeSum(TreeNode* root) { + dfs(root); + vector res; + for (auto& f : freq) { + if (f.second == maxFreq) res.push_back(f.first); + } + return res; + } + int dfs(TreeNode* root) { + if (!root) return 0; + int s = dfs(root->left) + dfs(root->right) + root->val; + maxFreq = max(maxFreq, ++freq[s]); + return s; + } +}; diff --git a/leetcode/problems/cpp/partitioning-into-minimum-number-of-deci-binary-numbers.cpp b/leetcode/problems/cpp/partitioning-into-minimum-number-of-deci-binary-numbers.cpp index 97d313d..ade9e43 100644 --- a/leetcode/problems/cpp/partitioning-into-minimum-number-of-deci-binary-numbers.cpp +++ b/leetcode/problems/cpp/partitioning-into-minimum-number-of-deci-binary-numbers.cpp @@ -1,12 +1,17 @@ // by @codeAbinash -// Time Complexity : O(n) -// Space Complexity : O(1) +// Time : O(n) +// Space : O(1) + +#include "string" +#include "algorithm" +using namespace std; class Solution { - public: - int minPartitions(string n) { - char m = '0'; - for (auto &i : n) m = max(m, i); - return m - '0'; - } -}; \ No newline at end of file +public: + int minPartitions(string n) { + int ans = 0; + for (auto ch : n) + ans = max(ans, ch - '0'); + return ans; + } +}; diff --git a/leetcode/problems/cpp/pseudo-palindromic-paths-in-a-binary-tree.cpp b/leetcode/problems/cpp/pseudo-palindromic-paths-in-a-binary-tree.cpp new file mode 100644 index 0000000..6594aa7 --- /dev/null +++ b/leetcode/problems/cpp/pseudo-palindromic-paths-in-a-binary-tree.cpp @@ -0,0 +1,30 @@ +// 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 ans = 0; +public: + int pseudoPalindromicPaths(TreeNode* root) { + if (!root) return 0; + dfs(root, 0); + return ans; + } + void dfs(TreeNode* root, int mask) { + if (!root) return; + mask ^= (1 << root->val); + if (!root->left && !root->right) + if ((mask & (mask - 1)) == 0) ans++; + dfs(root->left, mask); + dfs(root->right, mask); + } +}; diff --git a/leetcode/problems/cpp/recover-a-tree-from-preorder-traversal.cpp b/leetcode/problems/cpp/recover-a-tree-from-preorder-traversal.cpp new file mode 100644 index 0000000..936486a --- /dev/null +++ b/leetcode/problems/cpp/recover-a-tree-from-preorder-traversal.cpp @@ -0,0 +1,36 @@ +// by @codeAbinash +// Time : O(n) +// Space : O(n) + +#include "string" +#include "stack" +#include "vector" +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* recoverFromPreorder(string S) { + vector st; + for (int i = 0, level, val; i < S.length();) { + for (level = 0; S[i] == '-'; i++) level++; + for (val = 0; i < S.length() && S[i] != '-'; i++) + val = val * 10 + S[i] - '0'; + TreeNode* node = new TreeNode(val); + while (st.size() > level) st.pop_back(); + if (!st.empty()) + if (!st.back()->left) st.back()->left = node; + else st.back()->right = node; + st.push_back(node); + } + return st[0]; + } +}; diff --git a/leetcode/problems/cpp/spiral-matrix-iv.cpp b/leetcode/problems/cpp/spiral-matrix-iv.cpp new file mode 100644 index 0000000..4638c0e --- /dev/null +++ b/leetcode/problems/cpp/spiral-matrix-iv.cpp @@ -0,0 +1,31 @@ +// by @codeAbinash +// Time : O(n) +// Space : O(n) + +#include "vector" +using namespace std; + +struct ListNode { + int val; + ListNode* next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode* next) : val(x), next(next) {} +}; + +class Solution { +public: + vector> spiralMatrix(int m, int n, ListNode* head) { + int i = 0, j = 0, cur_d = 0, d[5] = { 0, 1, 0, -1, 0 }; + vector> res(m, vector(n, -1)); + for (; head != nullptr; head = head->next) { + res[i][j] = head->val; + int ni = i + d[cur_d], nj = j + d[cur_d + 1]; + if (min(ni, nj) < 0 || ni >= m || nj >= n || res[ni][nj] != -1) + cur_d = (cur_d + 1) % 4; + i += d[cur_d]; + j += d[cur_d + 1]; + } + return res; + } +}; diff --git a/leetcode/problems/cpp/sum-of-root-to-leaf-binary-numbers.cpp b/leetcode/problems/cpp/sum-of-root-to-leaf-binary-numbers.cpp new file mode 100644 index 0000000..1ec6398 --- /dev/null +++ b/leetcode/problems/cpp/sum-of-root-to-leaf-binary-numbers.cpp @@ -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 { +public: + int sumRootToLeaf(TreeNode* root) { + return dfs(root, 0); + } + int dfs(TreeNode* root, int sum) { + if (!root) return 0; + sum = sum << 1 | root->val; + if (!root->left && !root->right) return sum; + return dfs(root->left, sum) + dfs(root->right, sum); + } +}; diff --git a/leetcode/problems/cpp/trim-a-binary-search-tree.cpp b/leetcode/problems/cpp/trim-a-binary-search-tree.cpp new file mode 100644 index 0000000..6341af3 --- /dev/null +++ b/leetcode/problems/cpp/trim-a-binary-search-tree.cpp @@ -0,0 +1,27 @@ +// 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* trimBST(TreeNode* root, int low, int high) { + if (!root) return nullptr; + + root->left = trimBST(root->left, low, high); + root->right = trimBST(root->right, low, high); + + if (root->val < low) return root->right; + if (root->val > high) return root->left; + + return root; + } +}; diff --git a/leetcode/problems/cpp/univalued-binary-tree.cpp b/leetcode/problems/cpp/univalued-binary-tree.cpp new file mode 100644 index 0000000..da495e0 --- /dev/null +++ b/leetcode/problems/cpp/univalued-binary-tree.cpp @@ -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 { +public: + bool isUnivalTree(TreeNode* root) { + if (!root) return false; + return check(root, root->val); + } + bool check(TreeNode* root, int val) { + if (!root) return true; + if (root->val != val) return false; + return check(root->left, val) && check(root->right, val); + } +};