diff --git a/leetcode/301-400/0310.Minimum-Height-Trees/README.md b/leetcode/301-400/0310.Minimum-Height-Trees/README.md index eca86e20c..5c65617b7 100644 --- a/leetcode/301-400/0310.Minimum-Height-Trees/README.md +++ b/leetcode/301-400/0310.Minimum-Height-Trees/README.md @@ -1,28 +1,32 @@ # [310.Minimum Height Trees][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree. + +Given a tree of `n` nodes labelled from `0` to `n - 1`, and an array of `n - 1` `edges` where `edges[i] = [ai, bi]` indicates that there is an undirected edge between the two nodes `ai` and `bi` in the tree, you can choose any node of the tree as the root. When you select a node `x` as the root, the result tree has height `h`. Among all possible rooted trees, those with minimum height (i.e. `min(h)`) are called **minimum height trees** (MHTs). + +Return a list of all **MHTs'** root labels. You can return the answer in **any order**. + +The **height** of a rooted tree is the number of edges on the longest downward path between the root and a leaf. -**Example 1:** +**Example 1:** + +![1](./e1.jpeg) ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 4, edges = [[1,0],[1,2],[1,3]] +Output: [1] +Explanation: As shown, the height of the tree is 1 when the root is the node with label 1 which is the only MHT. ``` -## 题意 -> ... +**Example 2:** -## 题解 +![2](./e2.jpeg) -### 思路1 -> ... -Minimum Height Trees -```go ``` - +Input: n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]] +Output: [3,4] +``` ## 结语 diff --git a/leetcode/301-400/0310.Minimum-Height-Trees/Solution.go b/leetcode/301-400/0310.Minimum-Height-Trees/Solution.go index d115ccf5e..61dd8d67a 100644 --- a/leetcode/301-400/0310.Minimum-Height-Trees/Solution.go +++ b/leetcode/301-400/0310.Minimum-Height-Trees/Solution.go @@ -1,5 +1,39 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int, edges [][]int) []int { + if n == 1 { + return []int{0} + } + adj := make([]map[int]struct{}, n) + for i := 0; i < n; i++ { + adj[i] = make(map[int]struct{}) + } + for _, edge := range edges { + a, b := edge[0], edge[1] + adj[a][b] = struct{}{} + adj[b][a] = struct{}{} + } + l := []int{} + for i := 0; i < n; i++ { + if len(adj[i]) == 1 { + l = append(l, i) + } + } + r := n + for r > 2 { + r -= len(l) + nl := []int{} + for _, lf := range l { + var nei int + for n := range adj[lf] { + nei = n + } + delete(adj[nei], lf) + if len(adj[nei]) == 1 { + nl = append(nl, nei) + } + } + l = nl + } + return l } diff --git a/leetcode/301-400/0310.Minimum-Height-Trees/Solution_test.go b/leetcode/301-400/0310.Minimum-Height-Trees/Solution_test.go index 14ff50eb4..f340e14ac 100644 --- a/leetcode/301-400/0310.Minimum-Height-Trees/Solution_test.go +++ b/leetcode/301-400/0310.Minimum-Height-Trees/Solution_test.go @@ -10,30 +10,34 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + n int + edges [][]int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 4, [][]int{ + {1, 0}, {1, 2}, {1, 3}, + }, []int{1}}, + {"TestCase2", 6, [][]int{ + {3, 0}, {3, 1}, {3, 2}, {3, 4}, {5, 4}, + }, []int{3, 4}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.n, c.edges) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.n, c.edges) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { } diff --git a/leetcode/301-400/0310.Minimum-Height-Trees/e1.jpeg b/leetcode/301-400/0310.Minimum-Height-Trees/e1.jpeg new file mode 100644 index 000000000..28517fd68 Binary files /dev/null and b/leetcode/301-400/0310.Minimum-Height-Trees/e1.jpeg differ diff --git a/leetcode/301-400/0310.Minimum-Height-Trees/e2.jpeg b/leetcode/301-400/0310.Minimum-Height-Trees/e2.jpeg new file mode 100644 index 000000000..62f9f6bf6 Binary files /dev/null and b/leetcode/301-400/0310.Minimum-Height-Trees/e2.jpeg differ