Skip to content

Commit

Permalink
Added some solution
Browse files Browse the repository at this point in the history
  • Loading branch information
codeAbinash committed Aug 29, 2023
1 parent ca56be4 commit e5b76dc
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
42 changes: 42 additions & 0 deletions leetcode/problems/cpp/binary-search-tree-iterator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// by @codeAbinash
// Time : O(n)
// Space : O(n)

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

class BSTIterator {
stack<TreeNode*> st;
void partInorder(TreeNode* root) {
if (!root) return;
while (root) {
st.push(root);
root = root->left;
}
}

public:
BSTIterator(TreeNode* root) {
partInorder(root);
}

int next() {
TreeNode* top = st.top();
st.pop();
if (top->right) partInorder(top->right);
return top->val;
}

bool hasNext() {
return !st.empty();
}
};
48 changes: 48 additions & 0 deletions leetcode/problems/cpp/binary-tree-zigzag-level-order-traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// by @codeAbinash
// Time : O(n)
// Space : O(n)

#include "vector"
#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>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> ans;
queue<TreeNode*> q;
bool dir = true;

if (!root) return ans;
q.push(root);
vector<int> tmp;

while (!q.empty()) {
int size = q.size();
while (size--) {
TreeNode* front = q.front();
tmp.push_back(front->val);
q.pop();
if (front->left) q.push(front->left);
if (front->right) q.push(front->right);
}
if (dir = !dir)
reverse(tmp.begin(), tmp.end());
ans.push_back(tmp);
tmp.clear();
}

return ans;
}
};
30 changes: 30 additions & 0 deletions leetcode/problems/cpp/count-complete-tree-nodes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// by @codeAbinash
// Time : O(log n * log n)
// Space : O(1)

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 countNodes(TreeNode* root) {
if (!root) return 0;
TreeNode* left = root, * right = root;
int height = 0;

while (right) {
left = left->left;
right = right->right;
height++;
}

if (!left) return (1 << height) - 1;
return 1 + countNodes(root->left) + countNodes(root->right);
}
};

0 comments on commit e5b76dc

Please sign in to comment.