diff --git a/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/1.png b/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/1.png new file mode 100644 index 000000000..a8eb208f4 Binary files /dev/null and b/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/1.png differ diff --git a/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/2.png b/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/2.png new file mode 100644 index 000000000..f9f3c9fb5 Binary files /dev/null and b/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/2.png differ diff --git a/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/3.jpg b/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/3.jpg new file mode 100644 index 000000000..78cf21ae7 Binary files /dev/null and b/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/3.jpg differ diff --git a/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/README.md b/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/README.md index 5ef59eb3f..315daaf98 100755 --- a/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/README.md +++ b/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/README.md @@ -1,28 +1,43 @@ # [3249.Count the Number of Good Nodes][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 +There is an **undirected** tree with `n` nodes labeled from `0` to `n - 1`, and rooted at node `0`. You are given a 2D integer array `edges` of length `n - 1`, where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the tree. + +A node is **good** if all the subtrees rooted at its children have the same size. + +Return the number of **good** nodes in the given tree. + +A **subtree** of `treeName` is a tree consisting of a node in `treeName` and all of its descendants. -**Example 1:** +**Example 1:** + +![1](./1.png) ``` -Input: a = "11", b = "1" -Output: "100" +Input: edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]] + +Output: 7 ``` -## 题意 -> ... +**Example 2:** -## 题解 +![2](./2.png) -### 思路1 -> ... -Count the Number of Good Nodes -```go ``` +Input: edges = [[0,1],[1,2],[2,3],[3,4],[0,5],[1,6],[2,7],[3,8]] +Output: 6 +``` + +**Example 3:** + +![3](./3.jpg) + +``` +Input: edges = [[0,1],[1,2],[1,3],[1,4],[0,5],[5,6],[6,7],[7,8],[0,9],[9,10],[9,12],[10,11]] + +Output: 12 +``` ## 结语 diff --git a/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/Solution.go b/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/Solution.go index d115ccf5e..0ded0369f 100644 --- a/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/Solution.go +++ b/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/Solution.go @@ -1,5 +1,44 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(edges [][]int) int { + var ans int + n := len(edges) + 1 + adj := make(map[int][]int) + for _, e := range edges { + adj[e[0]] = append(adj[e[0]], e[1]) + adj[e[1]] = append(adj[e[1]], e[0]) + } + size := make([][]int, n) + var dfs func(int, int) int + dfs = func(root, parent int) int { + cnt := 1 + for _, rel := range adj[root] { + if rel == parent { + continue + } + lz := dfs(rel, root) + cnt += lz + size[root] = append(size[root], lz) + } + return cnt + } + dfs(0, -1) + for _, ls := range size { + if len(ls) == 0 { + ans++ + continue + } + + cmp := ls[0] + i := 1 + for ; i < len(ls); i++ { + if cmp != ls[i] { + break + } + } + if i == len(ls) { + ans++ + } + } + return ans } diff --git a/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/Solution_test.go b/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/Solution_test.go index 14ff50eb4..667c0470f 100644 --- a/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/Solution_test.go +++ b/leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs [][]int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]int{{0, 1}, {0, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}}, 7}, + {"TestCase2", [][]int{{0, 1}, {1, 2}, {2, 3}, {3, 4}, {0, 5}, {1, 6}, {2, 7}, {3, 8}}, 6}, + {"TestCase3", [][]int{{0, 1}, {1, 2}, {1, 3}, {1, 4}, {0, 5}, {5, 6}, {6, 7}, {7, 8}, {0, 9}, {9, 10}, {9, 12}, {10, 11}}, 12}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }