-
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
e5b76dc
commit 5def2db
Showing
3 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
leetcode/problems/cpp/binary-tree-level-order-traversal-ii.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,86 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(n) | ||
|
||
#include "iostream" | ||
#include "vector" | ||
#include "stack" | ||
#include "queue" | ||
#include "algorithm" | ||
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<vector<int>> levelOrderBottom(TreeNode* root) { | ||
vector<vector<int>> ans; | ||
if (!root) return ans; | ||
|
||
queue<TreeNode*> q; | ||
q.push(root); | ||
|
||
vector<int> tmp; | ||
while (!q.empty()) { | ||
int size = q.size(); | ||
while (size--) { | ||
TreeNode* front = q.front(); | ||
q.pop(); | ||
tmp.push_back(front->val); | ||
if (front->left) q.push(front->left); | ||
if (front->right) q.push(front->right); | ||
} | ||
ans.push_back(tmp); | ||
tmp.clear(); | ||
} | ||
|
||
reverse(ans.begin(), ans.end()); | ||
return ans; | ||
} | ||
}; | ||
|
||
|
||
// Using a Stack | ||
|
||
class Solution { | ||
|
||
public: | ||
vector<vector<int>> levelOrderBottom(TreeNode* root) { | ||
vector<vector<int>> ans; | ||
stack<vector<int>> stk; | ||
if (!root) return ans; | ||
|
||
queue<TreeNode*> q; | ||
q.push(root); | ||
|
||
vector<int> tmp; | ||
while (!q.empty()) { | ||
int size = q.size(); | ||
while (size--) { | ||
TreeNode* front = q.front(); | ||
q.pop(); | ||
tmp.push_back(front->val); | ||
if (front->left) q.push(front->left); | ||
if (front->right) q.push(front->right); | ||
} | ||
stk.push(tmp); | ||
tmp.clear(); | ||
} | ||
|
||
while (!stk.empty()) { | ||
ans.push_back(stk.top()); | ||
stk.pop(); | ||
} | ||
return ans; | ||
} | ||
}; | ||
|
33 changes: 33 additions & 0 deletions
33
leetcode/problems/cpp/convert-sorted-array-to-binary-search-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,33 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(n) | ||
|
||
#include "iostream" | ||
#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 { | ||
TreeNode* makeTree(int left, int right, vector<int>& nums) { | ||
if (left >= right) return NULL; | ||
int mid = (left + right) / 2; | ||
TreeNode* newNode = new TreeNode(nums[mid]); | ||
newNode->left = makeTree(left, mid, nums); | ||
newNode->right = makeTree(mid + 1, right, nums); | ||
return newNode; | ||
} | ||
public: | ||
TreeNode* sortedArrayToBST(vector<int>& nums) { | ||
return makeTree(0, nums.size(), nums); | ||
} | ||
}; |
50 changes: 50 additions & 0 deletions
50
leetcode/problems/cpp/convert-sorted-list-to-binary-search-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,50 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(n) | ||
|
||
#include "iostream" | ||
#include "vector" | ||
|
||
|
||
|
||
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) {} | ||
}; | ||
|
||
|
||
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 { | ||
TreeNode* toBst(ListNode* start, ListNode* end) { | ||
if (start == end) return nullptr; | ||
ListNode* slow = start; | ||
ListNode* fast = start; | ||
|
||
while (fast != end && fast->next != end) { | ||
slow = slow->next; | ||
fast = fast->next->next; | ||
} | ||
|
||
TreeNode* head = new TreeNode(slow->val); | ||
head->left = toBst(start, slow); | ||
head->right = toBst(slow->next, end); | ||
return head; | ||
} | ||
|
||
public: | ||
TreeNode* sortedListToBST(ListNode* head) { | ||
if (!head) return nullptr; | ||
return toBst(head, nullptr); | ||
} | ||
}; |