Skip to content

Commit

Permalink
Added some solution
Browse files Browse the repository at this point in the history
  • Loading branch information
codeAbinash committed Sep 7, 2023
1 parent 9c324bc commit cdc7193
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
72 changes: 72 additions & 0 deletions leetcode/problems/cpp/all-elements-in-two-binary-search-trees.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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) {}
};

// Memory efficient
class Solution {
void left(TreeNode* root, stack<TreeNode*>& s) {
while (root) {
s.push(root);
root = root->left;
}
}
public:
vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
vector<int> result;
stack<TreeNode*> s1, s2;
left(root1, s1);
left(root2, s2);

while (!s1.empty() || !s2.empty()) {
stack<TreeNode*>& s = s1.empty() ?
s2 : s2.empty() ?
s1 : s1.top()->val < s2.top()->val ?
s1 : s2;
TreeNode* node = s.top(); s.pop();
result.push_back(node->val);
left(node->right, s);
}
return result;
}
};

// Time efficient
class Solution {
void inorder(TreeNode* root, vector<int>& result) {
if (!root) return;
inorder(root->left, result);
result.push_back(root->val);
inorder(root->right, result);
}
public:
vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
int i, j;
vector<int> r1, r2, result;

inorder(root1, r1);
inorder(root2, r2);

for (i = 0, j = 0; i < r1.size() && j < r2.size();) {
if (r1[i] < r2[j]) result.push_back(r1[i++]);
else result.push_back(r2[j++]);
}

while (i < r1.size()) result.push_back(r1[i++]);
while (j < r2.size()) result.push_back(r2[j++]);

return result;
}
};
38 changes: 38 additions & 0 deletions leetcode/problems/cpp/balance-a-binary-search-tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// by @codeAbinash
// Time : O(n)
// Space : O(n)

#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 {
vector<TreeNode*> sorted;
void inorder(TreeNode* root) {
if (!root) return;
inorder(root->left);
sorted.push_back(root);
inorder(root->right);
}
TreeNode* helper(int start, int end) {
if (start > end) return nullptr;
int mid = (start + end) / 2;
auto node = sorted[mid];
node->left = helper(start, mid - 1);
node->right = helper(mid + 1, end);
return node;
}
public:
TreeNode* balanceBST(TreeNode* root) {
inorder(root);
return helper(0, sorted.size() - 1);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// by @codeAbinash
// Time : O(n)
// Space : O(n)

#include "vector"
#include "climits"
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 {
int index = 0;
TreeNode* helper(vector<int>& arr, int parent_val) {
if (index >= arr.size() || arr[index] > parent_val)
return nullptr;
auto n = new TreeNode(arr[index++]);
n->left = helper(arr, n->val);
n->right = helper(arr, parent_val);
return n;
}
public:
TreeNode* bstFromPreorder(vector<int>& preorder) {
return helper(preorder, INT_MAX);
}
};

0 comments on commit cdc7193

Please sign in to comment.