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 27, 2023
1 parent 13a74e1 commit 14b4270
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
40 changes: 40 additions & 0 deletions leetcode/problems/cpp/binary-tree-level-order-traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

#include "vector"
#include "queue"
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>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if (!root) return res;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
vector<int> level;
while (size--) {
TreeNode* node = q.front();
q.pop();
level.push_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
res.push_back(level);
}
return res;
}
};
33 changes: 33 additions & 0 deletions leetcode/problems/cpp/minimum-distance-between-bst-nodes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

#include "climits"
int min(int a, int b) { return a > b ? b : a; }

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 minDiff = INT_MAX;
int prev = -1;
void inorder(TreeNode* root) {
if (!root) return;
inorder(root->left);
if (prev != -1) minDiff = min(minDiff, root->val - prev);
prev = root->val;
inorder(root->right);
}
public:
int minDiffInBST(TreeNode* root) {
inorder(root);
return minDiff;
}
};
23 changes: 23 additions & 0 deletions leetcode/problems/cpp/same-tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// by @codeAbinash
// Time : O(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:
bool isSameTree(TreeNode* p, TreeNode* q) {
if (!q && !p) return true;
if (!q || !p) return false;
if (p->val == q->val)
return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
return false;
}
};
26 changes: 26 additions & 0 deletions leetcode/problems/cpp/symmetric-tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// by @codeAbinash
// Time : O(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:
bool isSameTreeSymmetric(TreeNode* p, TreeNode* q) {
if (!q && !p) return true;
if (!q || !p) return false;
if (p->val == q->val)
return (isSameTreeSymmetric(p->left, q->right) && isSameTreeSymmetric(p->right, q->left));
return false;
}
bool isSymmetric(TreeNode* root) {
return isSameTreeSymmetric(root->left, root->right);
}
};

0 comments on commit 14b4270

Please sign in to comment.