Skip to content

Commit a6625ab

Browse files
committed
Add solution and test-cases for problem 3249
1 parent 31756cd commit a6625ab

File tree

6 files changed

+76
-22
lines changed

6 files changed

+76
-22
lines changed
Loading
Loading
Loading

leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/README.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
# [3249.Count the Number of Good Nodes][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
5+
6+
A node is **good** if all the subtrees rooted at its children have the same size.
7+
8+
Return the number of **good** nodes in the given tree.
9+
10+
A **subtree** of `treeName` is a tree consisting of a node in `treeName` and all of its descendants.
711

8-
**Example 1:**
12+
**Example 1:**
13+
14+
![1](./1.png)
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]
18+
19+
Output: 7
1320
```
1421

15-
## 题意
16-
> ...
22+
**Example 2:**
1723

18-
## 题解
24+
![2](./2.png)
1925

20-
### 思路1
21-
> ...
22-
Count the Number of Good Nodes
23-
```go
2426
```
27+
Input: edges = [[0,1],[1,2],[2,3],[3,4],[0,5],[1,6],[2,7],[3,8]]
2528
29+
Output: 6
30+
```
31+
32+
**Example 3:**
33+
34+
![3](./3.jpg)
35+
36+
```
37+
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]]
38+
39+
Output: 12
40+
```
2641

2742
## 结语
2843

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(edges [][]int) int {
4+
var ans int
5+
n := len(edges) + 1
6+
adj := make(map[int][]int)
7+
for _, e := range edges {
8+
adj[e[0]] = append(adj[e[0]], e[1])
9+
adj[e[1]] = append(adj[e[1]], e[0])
10+
}
11+
size := make([][]int, n)
12+
var dfs func(int, int) int
13+
dfs = func(root, parent int) int {
14+
cnt := 1
15+
for _, rel := range adj[root] {
16+
if rel == parent {
17+
continue
18+
}
19+
lz := dfs(rel, root)
20+
cnt += lz
21+
size[root] = append(size[root], lz)
22+
}
23+
return cnt
24+
}
25+
dfs(0, -1)
26+
for _, ls := range size {
27+
if len(ls) == 0 {
28+
ans++
29+
continue
30+
}
31+
32+
cmp := ls[0]
33+
i := 1
34+
for ; i < len(ls); i++ {
35+
if cmp != ls[i] {
36+
break
37+
}
38+
}
39+
if i == len(ls) {
40+
ans++
41+
}
42+
}
43+
return ans
544
}

leetcode/3201-3300/3249.Count-the-Number-of-Good-Nodes/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{0, 1}, {0, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}}, 7},
17+
{"TestCase2", [][]int{{0, 1}, {1, 2}, {2, 3}, {3, 4}, {0, 5}, {1, 6}, {2, 7}, {3, 8}}, 6},
18+
{"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},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)