Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a7017f4

Browse files
committed
Add solution and test-cases for problem 834
1 parent e8bf636 commit a7017f4

File tree

6 files changed

+78
-25
lines changed

6 files changed

+78
-25
lines changed

‎leetcode/801-900/0834.Sum-of-Distances-in-Tree/README.md‎

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
# [834.Sum of Distances in Tree][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 connected tree with n nodes labeled from `0` to `n - 1` and `n - 1` edges.
5+
6+
You are given the integer `n` and the array `edges` where `edges[i] = [ai, bi]` indicates that there is an edge between nodes a<sub>i</sub> and b<sub>i</sub> in the tree.
7+
8+
Return an array `answer` of length `n` where `answer[i]` is the sum of the distances between the i<sup>th</sup> node in the tree and all other nodes.
79

8-
**Example 1:**
10+
**Example 1:**
11+
12+
![example1](./lc-sumdist1.jpg)
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: n = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]]
16+
Output: [8,12,6,10,10,10]
17+
Explanation: The tree is shown above.
18+
We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5)
19+
equals 1 +たす 1 +たす 2 +たす 2 +たす 2 = 8.
20+
Hence, answer[0] = 8, and so on.
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
1724

18-
## 题解
25+
![example2](./lc-sumdist2.jpg)
1926

20-
### 思路1
21-
> ...
22-
Sum of Distances in Tree
23-
```go
27+
```
28+
Input: n = 1, edges = []
29+
Output: [0]
2430
```
2531

32+
**Example 3:**
33+
34+
![example3](./lc-sumdist3.jpg)
35+
36+
```
37+
Input: n = 2, edges = [[1,0]]
38+
Output: [1,1]
39+
```
2640

2741
## 结语
2842

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

3-
func Solution(x bool) bool {
4-
return x
3+
// 借鉴官方算法
4+
func Solution(n int, edges [][]int) []int {
5+
adj := make(map[int]map[int]struct{})
6+
for u := 0; u < n; u++ {
7+
adj[u] = make(map[int]struct{})
8+
}
9+
for _, e := range edges {
10+
adj[e[0]][e[1]] = struct{}{}
11+
adj[e[1]][e[0]] = struct{}{}
12+
}
13+
14+
ans := make([]int, n)
15+
count := make([]int, n)
16+
for i := 0; i < n; i++ {
17+
count[i] = 1
18+
}
19+
var dfs func(int, int)
20+
var dfs1 func(int, int)
21+
dfs = func(node, parent int) {
22+
for child := range adj[node] {
23+
if child != parent {
24+
dfs(child, node)
25+
count[node] += count[child]
26+
ans[node] += ans[child] + count[child]
27+
}
28+
}
29+
}
30+
dfs1 = func(node, parent int) {
31+
for child := range adj[node] {
32+
if child != parent {
33+
ans[child] = ans[node] - count[child] + n - count[child]
34+
dfs1(child, node)
35+
}
36+
}
37+
}
38+
dfs(0, -1)
39+
dfs1(0, -1)
40+
return ans
541
}

‎leetcode/801-900/0834.Sum-of-Distances-in-Tree/Solution_test.go‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,33 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n int
14+
edges [][]int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 6, [][]int{{0, 1}, {0, 2}, {2, 3}, {2, 4}, {2, 5}}, []int{8, 12, 6, 10, 10, 10}},
18+
{"TestCase2", 1, [][]int{}, []int{0}},
19+
{"TestCase3", 2, [][]int{{1, 0}}, []int{1, 1}},
20+
{"TestCase4", 4, [][]int{{2, 0}, {3, 1}, {2, 1}}, []int{6, 4, 4, 6}},
21+
{"TestCase5", 5, [][]int{{0, 4}, {1, 3}, {1, 2}, {0, 2}}, []int{7, 7, 6, 10, 10}},
1922
}
2023

2124
// 开始测试
2225
for i, c := range cases {
2326
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
27+
got := Solution(c.n, c.edges)
2528
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
29+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
30+
c.expect, got, c.n, c.edges)
2831
}
2932
})
3033
}
3134
}
3235

33-
//压力测试
36+
//压力测试
3437
func BenchmarkSolution(b *testing.B) {
3538
}
3639

37-
//使用案列
40+
//使用案列
3841
func ExampleSolution() {
3942
}
11.9 KB
Loading[フレーム]
2.36 KB
Loading[フレーム]
4.33 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /