Skip to content

Commit b001289

Browse files
committed
GFG and LeetCode
1 parent fdbf78d commit b001289

6 files changed

+186
-0
lines changed

GeeksForGeeks/Easy/BFS of graph.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution
2+
{
3+
public:
4+
//Function to return Breadth First Traversal of given graph.
5+
vector<int> bfsOfGraph(int V, vector<int> adj[])
6+
{
7+
int visited[V + 1];
8+
vector<int> ans;
9+
memset(visited, 0, sizeof(visited));
10+
queue<int> q;
11+
q.push(0);
12+
visited[0] = 1;
13+
while (!q.empty())
14+
{
15+
int node = q.front();
16+
ans.push_back(node);
17+
q.pop();
18+
for (auto v : adj[node])
19+
{
20+
if (!visited[v])
21+
{
22+
visited[v] = 1;
23+
q.push(v);
24+
}
25+
}
26+
}
27+
28+
return ans;
29+
}
30+
};

GeeksForGeeks/Easy/DFS of Graph.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution
2+
{
3+
public:
4+
//Function to return a list containing the DFS traversal of the graph.
5+
void dfs(vector<int> adj[], int v, int visited[], vector<int> &ans)
6+
{
7+
visited[v] = 1;
8+
ans.push_back(v);
9+
for (auto i : adj[v])
10+
{
11+
if (visited[i] == false)
12+
{
13+
dfs(adj, i, visited, ans);
14+
}
15+
}
16+
}
17+
18+
vector<int> dfsOfGraph(int V, vector<int> adj[])
19+
{
20+
int visited[V];
21+
memset(visited, 0, sizeof(visited));
22+
vector<int> ans;
23+
dfs(adj, 0, visited, ans);
24+
return ans;
25+
}
26+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// return the Kth largest element in the given BST rooted at 'root'
2+
class Solution
3+
{
4+
public:
5+
void inorder(Node *root, int &k, int &ans)
6+
{
7+
if (root == NULL)
8+
return;
9+
inorder(root->right, k, ans);
10+
k--;
11+
if (k == 0)
12+
{
13+
ans = root->data;
14+
return;
15+
}
16+
inorder(root->left, k, ans);
17+
}
18+
19+
int kthLargest(Node *root, int K)
20+
{
21+
int ans = 0;
22+
inorder(root, K, ans);
23+
return ans;
24+
}
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution
2+
{
3+
public:
4+
//Function to detect cycle in a directed graph.
5+
bool dfs(vector<int> adj[], bool visited[], bool recStack[], int src)
6+
{
7+
visited[src] = true;
8+
recStack[src] = true;
9+
for (int it : adj[src])
10+
{
11+
if (recStack[it])
12+
{
13+
return true;
14+
}
15+
else if (!visited[it] && dfs(adj, visited, recStack, it))
16+
{
17+
return true;
18+
}
19+
}
20+
recStack[src] = false;
21+
return false;
22+
}
23+
24+
bool isCyclic(int V, vector<int> adj[])
25+
{
26+
bool visited[V] = {false};
27+
bool recStack[V] = {false};
28+
29+
for (int i = 0; i < V; ++i)
30+
{
31+
if (!visited[i] && dfs(adj, visited, recStack, i))
32+
{
33+
return true;
34+
}
35+
}
36+
return false;
37+
}
38+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution
2+
{
3+
public:
4+
//Function to detect cycle in an undirected graph.
5+
bool dfs(vector<int> adj[], int src, vector<bool> &visited, int parent)
6+
{
7+
visited[src] = true;
8+
for (auto j : adj[src])
9+
{
10+
if (!visited[j])
11+
{
12+
if (dfs(adj, j, visited, src))
13+
{
14+
return true;
15+
}
16+
}
17+
else if (j != parent)
18+
{
19+
return true;
20+
}
21+
}
22+
return false;
23+
}
24+
25+
bool isCycle(int V, vector<int> adj[])
26+
{
27+
vector<bool> visited(V);
28+
for (int i = 0; i < V; ++i)
29+
{
30+
if (!visited[i] && dfs(adj, i, visited, -1))
31+
{
32+
return true;
33+
}
34+
}
35+
return false;
36+
}
37+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution
13+
{
14+
public:
15+
TreeNode *mergeTrees(TreeNode *root1, TreeNode *root2)
16+
{
17+
if (root1 == nullptr)
18+
{
19+
return root2;
20+
}
21+
if (root2 == nullptr)
22+
{
23+
return root1;
24+
}
25+
root1->val = root1->val + root2->val;
26+
root1->left = mergeTrees(root1->left, root2->left);
27+
root1->right = mergeTrees(root1->right, root2->right);
28+
return root1;
29+
}
30+
};

0 commit comments

Comments
 (0)