Skip to content

Commit

Permalink
Added some code
Browse files Browse the repository at this point in the history
  • Loading branch information
codeAbinash committed Aug 30, 2023
1 parent e5b76dc commit 5def2db
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 0 deletions.
86 changes: 86 additions & 0 deletions leetcode/problems/cpp/binary-tree-level-order-traversal-ii.cpp
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;
}
};

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);
}
};
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);
}
};

0 comments on commit 5def2db

Please sign in to comment.