From 2d3a33547e1de522843fd3547cb4caf3fb56cac0 Mon Sep 17 00:00:00 2001 From: Lotnoy111 <1104117448@qq.com> Date: Sun, 23 Nov 2025 21:29:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Solution20.java=E4=BF=AE=E5=A4=8D5?= =?UTF-8?q?=E5=A4=84=E8=AF=AD=E6=B3=95=E9=94=99=E8=AF=AF=EF=BC=88=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E5=A3=B0=E6=98=8E/=E5=88=86=E5=8F=B7/=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E8=B0=83=E7=94=A8=E7=AD=89=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- L2023111042_20_Test.java | 69 ++++++++++++++++++++++++++++++++++++++++ Solution20.java | 18 +++++++---- 2 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 L2023111042_20_Test.java diff --git a/L2023111042_20_Test.java b/L2023111042_20_Test.java new file mode 100644 index 00000000..392d77a9 --- /dev/null +++ b/L2023111042_20_Test.java @@ -0,0 +1,69 @@ +import org.junit.jupiter.api.Test; +import java.util.List; +import static org.junit.jupiter.api.Assertions.*; + +// 命名规则:L学号_20_Test.java(需将“2023001”替换为自己的学号) +class L2023001_20_Test { + private final Solution solution = new Solution(); + + // 测试目的:验证单节点场景(n=1,无边);测试用例:n=1,edges=[], 预期输出[0] + @Test + void testSingleNode() { + int n = 1; + int[][] edges = {}; + List result = solution.findMinHeightTrees(n, edges); + List expected = List.of(0); + assertEquals(expected, result, "单节点树的最小高度根节点应为[0]"); + } + + // 测试目的:验证两节点场景(n=2,一条边);测试用例:n=2,edges=[[0,1]], 预期输出[0,1] + @Test + void testTwoNodes() { + int n = 2; + int[][] edges = {{0, 1}}; + List result = solution.findMinHeightTrees(n, edges); + List expected = List.of(0, 1); + // 忽略顺序(题目允许任意顺序返回) + assertTrue(result.containsAll(expected) && expected.containsAll(result), "两节点树的最小高度根节点应为[0,1]"); + } + + // 测试目的:验证示例1(星形结构);测试用例:n=4,edges=[[1,0],[1,2],[1,3]], 预期输出[1] + @Test + void testExample1() { + int n = 4; + int[][] edges = {{1, 0}, {1, 2}, {1, 3}}; + List result = solution.findMinHeightTrees(n, edges); + List expected = List.of(1); + assertEquals(expected, result, "示例1的最小高度根节点应为[1]"); + } + + // 测试目的:验证示例2(分支结构);测试用例:n=6,edges=[[3,0],[3,1],[3,2],[3,4],[5,4]], 预期输出[3,4] + @Test + void testExample2() { + int n = 6; + int[][] edges = {{3, 0}, {3, 1}, {3, 2}, {3, 4}, {5, 4}}; + List result = solution.findMinHeightTrees(n, edges); + List expected = List.of(3, 4); + assertTrue(result.containsAll(expected) && expected.containsAll(result), "示例2的最小高度根节点应为[3,4]"); + } + + // 测试目的:验证链状树(n=5,线性结构);测试用例:n=5,edges=[[0,1],[1,2],[2,3],[3,4]], 预期输出[2] + @Test + void testChainTree() { + int n = 5; + int[][] edges = {{0, 1}, {1, 2}, {2, 3}, {3, 4}}; + List result = solution.findMinHeightTrees(n, edges); + List expected = List.of(2); + assertEquals(expected, result, "链状树(n=5)的最小高度根节点应为[2]"); + } + + // 测试目的:验证三叉树(n=5,中间节点连4个子节点);测试用例:n=5,edges=[[2,0],[2,1],[2,3],[2,4]], 预期输出[2] + @Test + void testThreeBranchTree() { + int n = 5; + int[][] edges = {{2, 0}, {2, 1}, {2, 3}, {2, 4}}; + List result = solution.findMinHeightTrees(n, edges); + List expected = List.of(2); + assertEquals(expected, result, "三叉树(n=5)的最小高度根节点应为[2]"); + } +} \ No newline at end of file diff --git a/Solution20.java b/Solution20.java index f9e5089c..89f90181 100644 --- a/Solution20.java +++ b/Solution20.java @@ -16,7 +16,6 @@ * 示例 2: * 输入:n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]] * 输出:[3,4] - */ class Solution { public List findMinHeightTrees(int n, int[][] edges) { @@ -25,7 +24,8 @@ public List findMinHeightTrees(int n, int[][] edges) { ans.add(0); return ans; } - List[]() adj == new List[n]; + // 修复1:数组声明+赋值语法错误 + List[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList(); } @@ -34,20 +34,23 @@ public List findMinHeightTrees(int n, int[][] edges) { adj[edge[1]].add(edge[0]); } - int[] parent = new int[n] + // 修复2:缺少分号 + int[] parent = new int[n]; Arrays.fill(parent, -1); /* 找到与节点 0 最远的节点 x */ int x = findLongestNode(0, parent, adj); /* 找到与节点 x 最远的节点 y */ int y = findLongestNode(x, parent, adj); /* 求出节点 x 到节点 y 的路径 */ - List path[] = new ArrayList(); + // 修复3:path无需数组,改为普通List + List path = new ArrayList(); parent[x] = -1; while (y != -1) { path.add(y); y = parent[y]; } - int m = path.size; + // 修复4:size()方法调用(添加括号) + int m = path.size(); if (m % 2 == 0) { ans.add(path.get(m / 2 - 1)); } @@ -60,7 +63,8 @@ public int findLongestNode(int u, int[] parent, List[] adj) { Queue queue = new ArrayDeque(); boolean[] visit = new boolean[n]; queue.offer(u); - visit[u] === true; + // 修复5:赋值语法错误(===改为=) + visit[u] = true; int node = -1; while (!queue.isEmpty()) { @@ -76,4 +80,4 @@ public int findLongestNode(int u, int[] parent, List[] adj) { } return node; } -} +} \ No newline at end of file