Skip to content

Commit

Permalink
add khushal87 new code
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed Aug 26, 2021
1 parent 0ec1093 commit bb4c947
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"conio.h": "c",
"array": "cpp",
"string": "cpp",
"string_view": "cpp"
"string_view": "cpp",
"iostream": "cpp"
}
}
8 changes: 5 additions & 3 deletions BST/check_if_BST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ bool isBSTUtil(Node *root, int min, int max)
{
if (root == nullptr)
return true;
if (root->data < min || root->data > max)
return 0;
return isBSTUtil(root->left, min, root->data - 1) && isBSTUtil(root->right, root->data + 1, max);
if (root->data > min && root->data < max)
{
return isBSTUtil(root->left, min, root->data) && isBSTUtil(root->right, root->data, max);
}
return false;
}

bool isBST(Node *root)
Expand Down
1 change: 1 addition & 0 deletions Graph/finding_bridges.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void dfs(vector<vector<int>> &adj, vector<bool> &vis, vector<int> &in, vector<in
{
cout << i << " - " << j << " is a bridge" << endl;
}
low[i] = min(low[i], low[j]);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Heap/k_closest_point_to_origin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
using namespace std;
typedef pair<int, pair<int, int>> pppi;

void kClosestPoints(vector<pair<int, int>> points, int n)
void kClosestPoints(vector<pair<int, int>> points, int k)
{
priority_queue<pppi> pq;
for (auto j : points)
{
int dis = ((j.second * j.second)) + ((j.first * j.first));
pq.push({dis, j});
if (pq.size() > 0)
if (pq.size() > k)
{
pq.pop();
}
Expand Down
29 changes: 16 additions & 13 deletions Tree/is_subtree.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
#include <bits/stdc++.h>
using namespace std;

struct Tree
struct TreeNode
{
int val;
Tree *left;
Tree *right;
TreeNode *left;
TreeNode *right;
};

bool isIdentical(Tree *root1, Tree *root2)
bool areIdentical(TreeNode *root, TreeNode *subRoot)
{
if (root1 == nullptr && root2 == nullptr)
if (root == nullptr && subRoot == nullptr)
return true;
if (root1 == nullptr || root2 == nullptr)
if (root == nullptr || subRoot == nullptr)
return false;
return root1->val == root2->val && isIdentical(root1->left, root2->left) &&
isIdentical(root1->right, root2->right);
if (root->val == subRoot->val && areIdentical(root->left, subRoot->left) && areIdentical(root->right, subRoot->right))
{
return true;
}
return false;
}

bool solve(Tree *root, Tree *target)
bool isSubtree(TreeNode *root, TreeNode *subRoot)
{
if (target == nullptr)
return true;
if (root == nullptr)
return false;
if (isIdentical(root, target))
if (subRoot == nullptr)
return true;
if (areIdentical(root, subRoot))
return true;
return solve(root->left, target) || solve(root->right, target);
return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
}

0 comments on commit bb4c947

Please sign in to comment.