Skip to content

feat: add solutions to lc problem: No.310 #2451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 63 additions & 15 deletions solution/0300-0399/0310.Minimum Height Trees/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@

## 解法

### 方法一
### 方法一:拓扑排序

如果这棵树只有一个节点,那么这个节点就是最小高度树的根节点,直接返回这个节点即可。

如果这棵树有多个节点,那么一定存在叶子节点。叶子节点是只有一个相邻节点的节点。我们可以利用拓扑排序,从外向内剥离叶子节点,当我们到达最后一层的时候,剩下的节点就是最小高度树的根节点。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为节点数。

<!-- tabs:start -->

Expand All @@ -60,7 +66,7 @@ class Solution:
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
if n == 1:
return [0]
g = defaultdict(list)
g = [[] for _ in range(n)]
degree = [0] * n
for a, b in edges:
g[a].append(b)
Expand All @@ -85,7 +91,7 @@ class Solution:
class Solution {
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
if (n == 1) {
return Collections.singletonList(0);
return List.of(0);
}
List<Integer>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
Expand All @@ -97,7 +103,7 @@ class Solution {
++degree[a];
++degree[b];
}
Queue<Integer> q = new LinkedList<>();
Deque<Integer> q = new ArrayDeque<>();
for (int i = 0; i < n; ++i) {
if (degree[i] == 1) {
q.offer(i);
Expand Down Expand Up @@ -125,7 +131,9 @@ class Solution {
class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
if (n == 1) return {0};
if (n == 1) {
return {0};
}
vector<vector<int>> g(n);
vector<int> degree(n);
for (auto& e : edges) {
Expand All @@ -136,19 +144,23 @@ public:
++degree[b];
}
queue<int> q;
for (int i = 0; i < n; ++i)
if (degree[i] == 1)
for (int i = 0; i < n; ++i) {
if (degree[i] == 1) {
q.push(i);
}
}
vector<int> ans;
while (!q.empty()) {
ans.clear();
for (int i = q.size(); i > 0; --i) {
int a = q.front();
q.pop();
ans.push_back(a);
for (int b : g[a])
if (--degree[b] == 1)
for (int b : g[a]) {
if (--degree[b] == 1) {
q.push(b);
}
}
}
}
return ans;
Expand All @@ -157,7 +169,7 @@ public:
```

```go
func findMinHeightTrees(n int, edges [][]int) []int {
func findMinHeightTrees(n int, edges [][]int) (ans []int) {
if n == 1 {
return []int{0}
}
Expand All @@ -170,13 +182,12 @@ func findMinHeightTrees(n int, edges [][]int) []int {
degree[a]++
degree[b]++
}
var q []int
for i := 0; i < n; i++ {
if degree[i] == 1 {
q := []int{}
for i, d := range degree {
if d == 1 {
q = append(q, i)
}
}
var ans []int
for len(q) > 0 {
ans = []int{}
for i := len(q); i > 0; i-- {
Expand All @@ -191,7 +202,44 @@ func findMinHeightTrees(n int, edges [][]int) []int {
}
}
}
return ans
return
}
```

```ts
function findMinHeightTrees(n: number, edges: number[][]): number[] {
if (n === 1) {
return [0];
}
const g: number[][] = Array.from({ length: n }, () => []);
const degree: number[] = Array(n).fill(0);
for (const [a, b] of edges) {
g[a].push(b);
g[b].push(a);
++degree[a];
++degree[b];
}
const q: number[] = [];
for (let i = 0; i < n; ++i) {
if (degree[i] === 1) {
q.push(i);
}
}
const ans: number[] = [];
while (q.length > 0) {
ans.length = 0;
const t: number[] = [];
for (const a of q) {
ans.push(a);
for (const b of g[a]) {
if (--degree[b] === 1) {
t.push(b);
}
}
}
q.splice(0, q.length, ...t);
}
return ans;
}
```

Expand Down
78 changes: 63 additions & 15 deletions solution/0300-0399/0310.Minimum Height Trees/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@

## Solutions

### Solution 1
### Solution 1: Topological Sorting

If the tree only has one node, then this node is the root of the minimum height tree. We can directly return this node.

If the tree has multiple nodes, there must be leaf nodes. A leaf node is a node that only has one adjacent node. We can use topological sorting to peel off the leaf nodes from the outside to the inside. When we reach the last layer, the remaining nodes are the root nodes of the minimum height tree.

The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the number of nodes.

<!-- tabs:start -->

Expand All @@ -53,7 +59,7 @@ class Solution:
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
if n == 1:
return [0]
g = defaultdict(list)
g = [[] for _ in range(n)]
degree = [0] * n
for a, b in edges:
g[a].append(b)
Expand All @@ -78,7 +84,7 @@ class Solution:
class Solution {
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
if (n == 1) {
return Collections.singletonList(0);
return List.of(0);
}
List<Integer>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
Expand All @@ -90,7 +96,7 @@ class Solution {
++degree[a];
++degree[b];
}
Queue<Integer> q = new LinkedList<>();
Deque<Integer> q = new ArrayDeque<>();
for (int i = 0; i < n; ++i) {
if (degree[i] == 1) {
q.offer(i);
Expand Down Expand Up @@ -118,7 +124,9 @@ class Solution {
class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
if (n == 1) return {0};
if (n == 1) {
return {0};
}
vector<vector<int>> g(n);
vector<int> degree(n);
for (auto& e : edges) {
Expand All @@ -129,19 +137,23 @@ public:
++degree[b];
}
queue<int> q;
for (int i = 0; i < n; ++i)
if (degree[i] == 1)
for (int i = 0; i < n; ++i) {
if (degree[i] == 1) {
q.push(i);
}
}
vector<int> ans;
while (!q.empty()) {
ans.clear();
for (int i = q.size(); i > 0; --i) {
int a = q.front();
q.pop();
ans.push_back(a);
for (int b : g[a])
if (--degree[b] == 1)
for (int b : g[a]) {
if (--degree[b] == 1) {
q.push(b);
}
}
}
}
return ans;
Expand All @@ -150,7 +162,7 @@ public:
```

```go
func findMinHeightTrees(n int, edges [][]int) []int {
func findMinHeightTrees(n int, edges [][]int) (ans []int) {
if n == 1 {
return []int{0}
}
Expand All @@ -163,13 +175,12 @@ func findMinHeightTrees(n int, edges [][]int) []int {
degree[a]++
degree[b]++
}
var q []int
for i := 0; i < n; i++ {
if degree[i] == 1 {
q := []int{}
for i, d := range degree {
if d == 1 {
q = append(q, i)
}
}
var ans []int
for len(q) > 0 {
ans = []int{}
for i := len(q); i > 0; i-- {
Expand All @@ -184,7 +195,44 @@ func findMinHeightTrees(n int, edges [][]int) []int {
}
}
}
return ans
return
}
```

```ts
function findMinHeightTrees(n: number, edges: number[][]): number[] {
if (n === 1) {
return [0];
}
const g: number[][] = Array.from({ length: n }, () => []);
const degree: number[] = Array(n).fill(0);
for (const [a, b] of edges) {
g[a].push(b);
g[b].push(a);
++degree[a];
++degree[b];
}
const q: number[] = [];
for (let i = 0; i < n; ++i) {
if (degree[i] === 1) {
q.push(i);
}
}
const ans: number[] = [];
while (q.length > 0) {
ans.length = 0;
const t: number[] = [];
for (const a of q) {
ans.push(a);
for (const b of g[a]) {
if (--degree[b] === 1) {
t.push(b);
}
}
}
q.splice(0, q.length, ...t);
}
return ans;
}
```

Expand Down
16 changes: 11 additions & 5 deletions solution/0300-0399/0310.Minimum Height Trees/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
if (n == 1) return {0};
if (n == 1) {
return {0};
}
vector<vector<int>> g(n);
vector<int> degree(n);
for (auto& e : edges) {
Expand All @@ -12,19 +14,23 @@ class Solution {
++degree[b];
}
queue<int> q;
for (int i = 0; i < n; ++i)
if (degree[i] == 1)
for (int i = 0; i < n; ++i) {
if (degree[i] == 1) {
q.push(i);
}
}
vector<int> ans;
while (!q.empty()) {
ans.clear();
for (int i = q.size(); i > 0; --i) {
int a = q.front();
q.pop();
ans.push_back(a);
for (int b : g[a])
if (--degree[b] == 1)
for (int b : g[a]) {
if (--degree[b] == 1) {
q.push(b);
}
}
}
}
return ans;
Expand Down
11 changes: 5 additions & 6 deletions solution/0300-0399/0310.Minimum Height Trees/Solution.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func findMinHeightTrees(n int, edges [][]int) []int {
func findMinHeightTrees(n int, edges [][]int) (ans []int) {
if n == 1 {
return []int{0}
}
Expand All @@ -11,13 +11,12 @@ func findMinHeightTrees(n int, edges [][]int) []int {
degree[a]++
degree[b]++
}
var q []int
for i := 0; i < n; i++ {
if degree[i] == 1 {
q := []int{}
for i, d := range degree {
if d == 1 {
q = append(q, i)
}
}
var ans []int
for len(q) > 0 {
ans = []int{}
for i := len(q); i > 0; i-- {
Expand All @@ -32,5 +31,5 @@ func findMinHeightTrees(n int, edges [][]int) []int {
}
}
}
return ans
return
}
4 changes: 2 additions & 2 deletions solution/0300-0399/0310.Minimum Height Trees/Solution.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Solution {
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
if (n == 1) {
return Collections.singletonList(0);
return List.of(0);
}
List<Integer>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
Expand All @@ -13,7 +13,7 @@ public List<Integer> findMinHeightTrees(int n, int[][] edges) {
++degree[a];
++degree[b];
}
Queue<Integer> q = new LinkedList<>();
Deque<Integer> q = new ArrayDeque<>();
for (int i = 0; i < n; ++i) {
if (degree[i] == 1) {
q.offer(i);
Expand Down
Loading