-
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
9c324bc
commit cdc7193
Showing
3 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
leetcode/problems/cpp/all-elements-in-two-binary-search-trees.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,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; | ||
} | ||
}; |
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,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); | ||
} | ||
}; |
32 changes: 32 additions & 0 deletions
32
leetcode/problems/cpp/construct-binary-search-tree-from-preorder-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,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); | ||
} | ||
}; |