Skip to content

Commit

Permalink
add khushal87 new-codes
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed Mar 2, 2021
1 parent 14c625a commit 9e3e054
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 2 deletions.
Binary file removed Array/max_min_in_arrays/2
Binary file not shown.
Binary file removed Array/maximum_occured_integer_in_n_ranges.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion Graph/undirected_graph_has_cycle_or_not.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ int main()
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(adj, vis, 1, -1);
cout << dfs(adj, vis, 1, -1);
}
4 changes: 3 additions & 1 deletion Stack/maximum_area_histogram.c++
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ int maxAreaOfHistogram(int arr[], int n)
for (int i = 0; i < left.size(); i++)
{
width[i] = right[i] - left[i] - 1;
cout << width[i] << " ";
}
cout << endl;
vector<int> area;
for (int i = 0; i < n; i++)
{
Expand All @@ -96,6 +98,6 @@ int main()
{
cin >> arr[i];
}
maxAreaOfHistogram(arr, n);
cout << maxAreaOfHistogram(arr, n);
return 0;
}
26 changes: 26 additions & 0 deletions Stack/reverse_a_stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <bits/stdc++.h>
using namespace std;

void insertBottom(stack<int> &st, int x)
{
if (st.size() == 0)
st.push(x);
else
{
int temp = st.top();
st.pop();
insertBottom(st, x);
st.push(temp);
}
}

void reverse(stack<int> &st)
{
if (st.size() > 0)
{
int x = st.top();
st.pop();
reverse(st);
insertBottom(st, x);
}
}
26 changes: 26 additions & 0 deletions Stack/sort_a_stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <bits/stdc++.h>
using namespace std;

void insertBottom(stack<int> &st, int x)
{
if (st.size() == 0 || x > st.top())
st.push(x);
else
{
int temp = st.top();
st.pop();
insertBottom(st, x);
st.push(temp);
}
}

void reverse(stack<int> &st)
{
if (st.size() > 0)
{
int x = st.top();
st.pop();
reverse(st);
insertBottom(st, x);
}
}
36 changes: 36 additions & 0 deletions Tree/diagonal_order_traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
Node *left;
Node *right;
};

void getDiagonalOrder(Node *root, int hd, map<int, vector<int>> &mp)
{
if (root == nullptr)
return;
mp[hd].push_back(root->data);

//for left decrease by 1
getDiagonalOrder(root->left, hd - 1, mp);

//for right remains same
getDiagonalOrder(root->right, hd, mp);
}

void printDiagonalOrder(Node *root)
{
map<int, vector<int>> mp;
int hd = 0;
getDiagonalOrder(root, hd, mp);
for (auto j : mp)
{
for (auto k : j.second)
{
cout << k << " ";
}
}
}
30 changes: 30 additions & 0 deletions Tree/is_subtree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>
using namespace std;

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

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

bool solve(Tree *root, Tree *target)
{
if (target == nullptr)
return true;
if (root == nullptr)
return false;
if (isIdentical(root, target))
return true;
return solve(root->left, target) || solve(root->right, target);
}
27 changes: 27 additions & 0 deletions Tree/is_sum_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <bits/stdc++.h>
using namespace std;

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

bool solve(Tree *root)
{
if (root == nullptr || (root->left == nullptr && root->right == nullptr))
return 1;
else
{
int left = 0, right = 0;
if (root->left)
left = root->left->val;
if (root->right)
right = root->right->val;
if (((left + right) == root->val) && solve(root->left) && solve(root->right))
return true;
else
return false;
}
}
31 changes: 31 additions & 0 deletions Tree/tree_is_balanced_or_not.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
Node *left;
Node *right;
};

int height(Node *root)
{
if (root == nullptr)
return 0;
else
{
return 1 + max(height(root->left), height(root->right));
}
}

bool heightBalancedOrNot(Node *root)
{
if (root == nullptr)
return true;
int lHeight = height(root->left);
int rHeight = height(root->right);
if (abs(lHeight - rHeight) <= 1 && heightBalancedOrNot(root->left) && heightBalancedOrNot(root->right))
return true;
else
return false;
}

0 comments on commit 9e3e054

Please sign in to comment.