-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
180 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 << " "; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |