We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent b001289 commit 95eab30Copy full SHA for 95eab30
GeeksForGeeks/Easy/Print adjacency list.cpp
@@ -0,0 +1,19 @@
1
+class Solution
2
+{
3
+public:
4
+ //Function to return the adjacency list for each vertex.
5
+ vector<vector<int>> printGraph(int V, vector<int> adj[])
6
+ {
7
+ vector<vector<int>> v(V);
8
+ for (int i = 0; i < V; ++i)
9
10
+ v[i].push_back(i);
11
+ for (auto it : adj[i])
12
13
+ v[i].push_back(it);
14
+ }
15
16
+
17
+ return v;
18
19
+};
GeeksForGeeks/Easy/Reverse Level Order Traversal.cpp
@@ -0,0 +1,32 @@
+vector<int> reverseLevelOrder(Node *root)
+ vector<int> ans;
+ if (root == nullptr)
+ return ans;
+ queue<Node *> q;
+ stack<int> s;
+ q.push(root);
+ while (!q.empty())
+ Node *curr = q.front();
+ s.push(curr->data);
+ q.pop();
+ if (curr->right)
+ q.push(curr->right);
20
21
+ if (curr->left)
22
23
+ q.push(curr->left);
24
25
26
+ while (!s.empty())
27
28
+ ans.push_back(s.top());
29
+ s.pop();
30
31
32
+}
GeeksForGeeks/Easy/Sort an array of 0s, 1s and 2s.cpp
@@ -0,0 +1,26 @@
+ void sort012(int arr[], int n)
+ long int low = 0, mid = 0, high = n - 1;
+ while (mid <= high)
+ if (arr[mid] == 0)
+ swap(arr[mid], arr[low]);
+ low++;
+ mid++;
+ else if (arr[mid] == 1)
+ else
+ swap(arr[mid], arr[high]);
+ high--;
GeeksForGeeks/Medium/Find the number of islands.cpp
@@ -0,0 +1,42 @@
+ //Function to find the number of islands.
+ void dfs(vector<vector<char>> &grid, int n, int m, int i, int j)
+ if (i >= 0 && j >= 0 && i < n && j < m && grid[i][j] == '1')
+ grid[i][j] = '5';
+ dfs(grid, n, m, i - 1, j); //top
+ dfs(grid, n, m, i + 1, j); //down
+ dfs(grid, n, m, i, j - 1); //left side
+ dfs(grid, n, m, i, j + 1); //right side
+ dfs(grid, n, m, i - 1, j - 1); //north west
+ dfs(grid, n, m, i + 1, j + 1); //south east
+ dfs(grid, n, m, i - 1, j + 1); //north east
+ dfs(grid, n, m, i + 1, j - 1); //south west
+ int numIslands(vector<vector<char>> &grid)
+ int count = 0;
+ int n = grid.size();
+ int m = grid[0].size();
+ for (int i = 0; i < n; ++i)
+ for (int j = 0; j < m; ++j)
+ if (grid[i][j] == '1')
33
34
+ dfs(grid, n, m, i, j);
35
+ count++;
36
37
38
39
40
+ return count;
41
42
GeeksForGeeks/Medium/Topological sort.cpp
@@ -0,0 +1,45 @@
+ //Function to return list containing vertices in Topological order.
+ vector<int> topoSort(int V, vector<int> adj[])
+ vector<int> inorder(V, 0);
+ inorder[it]++;
+ queue<int> q;
+ if (inorder[i] == 0)
+ q.push(i);
+ int u = q.front();
+ ans.push_back(u);
+ for (auto v : adj[u])
+ inorder[v]--;
+ if (inorder[v] == 0)
+ q.push(v);
43
44
45
Leet Code/C++/102. Binary Tree Level Order Traversal.cpp
@@ -0,0 +1,47 @@
+/**
+ * Definition for a binary tree node.
+ * 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) {}
+ * };
+ */
+ vector<vector<int>> levelOrder(TreeNode *root)
+ vector<vector<int>> ans;
+ vector<int> v;
+ queue<TreeNode *> q;
+ int sz = q.size();
+ while (sz--)
+ TreeNode *curr = q.front();
+ v.push_back(curr->val);
+ ans.push_back(v);
+ v.clear();
46
47
Leet Code/C++/344. Reverse String.cpp
@@ -0,0 +1,15 @@
+ void reverseString(vector<char> &s)
+ int last = s.size() - 1;
+ int first = 0;
+ while (first < last)
+ swap(s[first], s[last]);
+ first++;
+ last--;
0 commit comments