Skip to content

Commit

Permalink
2024-08-10 Minimum Height Trees
Browse files Browse the repository at this point in the history
  • Loading branch information
InSange committed Aug 9, 2024
1 parent 843a861 commit 5fd4958
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
66 changes: 66 additions & 0 deletions InSange/BFS/310_Minimum Height Trees.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <vector>
#include <map>
#include <queue>

using namespace std;

class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
map<int, vector<int>> m;
vector<int> ans;
vector<bool> check(n, false);
int* degree = new int[n] {0};

for (vector<int> node : edges)
{
m[node[0]].push_back(node[1]);
m[node[1]].push_back(node[0]);
degree[node[0]]++;
degree[node[1]]++;
}
int current_n = n;

queue<int> q;
if (current_n > 2)
{
for (int i = 0; i < n; i++)
{
if (degree[i] == 1 && check[i] == false)
{
check[i] = true;
q.push(i);
current_n--;
}
}
}
while (current_n > 2 && !q.empty())
{
int size = q.size();

for (int j = 0; j < size; j++)
{
int remove_n = q.front();
q.pop();

for (auto node : m[remove_n])
{
degree[node]--;
if (degree[node] == 1)
{
q.push(node);
check[node] = true;
current_n--;
}
}
}
}

for (int i = 0; i < n; i++)
{
if (check[i] == false) ans.push_back(i);
}

return ans;
}
};
1 change: 1 addition & 0 deletions InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
| 22์ฐจ์‹œ | 2024.07.31 | DP | [Filling Bookcase Shelves](https://leetcode.com/problems/filling-bookcase-shelves/) | [#22](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/81)]
| 23์ฐจ์‹œ | 2024.08.03 | ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ | [Minimum Swaps to Group All 1's Together 2](https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii/) | [#23](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/82)]
| 24์ฐจ์‹œ | 2024.08.04 | BFS | [ํŠธ๋ฆฌ](https://www.acmicpc.net/problem/1068) | [#24](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/83)]
| 25์ฐจ์‹œ | 2024.08.10 | BFS | [Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/) | [#25](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/87)]
---

https://leetcode.com/problems/robot-collisions/

0 comments on commit 5fd4958

Please sign in to comment.