Skip to content

Commit ffe3acc

Browse files
authored
Merge pull request #853 from 0xff-dev/2641
Add solution and test-cases for problem 2641
2 parents ec47cff + 1dc23dd commit ffe3acc

File tree

5 files changed

+116
-9
lines changed

5 files changed

+116
-9
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# [1001.Grid Illumination][title]
2+
3+
## Description
4+
Given the `root` of a binary tree, replace the value of each node in the tree with the **sum of all its cousins' values**.
5+
6+
Two nodes of a binary tree are **cousins** if they have the same depth with different parents.
7+
8+
Return the `root` of the modified tree.
9+
10+
**Note** that the depth of a node is the number of edges in the path from the root node to it.
11+
12+
**Example 1:**
13+
14+
![1](./example11.png)
15+
16+
```
17+
Input: root = [5,4,9,1,10,null,7]
18+
Output: [0,0,0,7,7,null,11]
19+
Explanation: The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
20+
- Node with value 5 does not have any cousins so its sum is 0.
21+
- Node with value 4 does not have any cousins so its sum is 0.
22+
- Node with value 9 does not have any cousins so its sum is 0.
23+
- Node with value 1 has a cousin with value 7 so its sum is 7.
24+
- Node with value 10 has a cousin with value 7 so its sum is 7.
25+
- Node with value 7 has cousins with values 1 and 10 so its sum is 11.
26+
```
27+
28+
**Example 2:**
29+
30+
![2](./diagram33.png)
31+
32+
```
33+
Input: root = [3,1,2]
34+
Output: [0,0,0]
35+
Explanation: The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
36+
- Node with value 3 does not have any cousins so its sum is 0.
37+
- Node with value 1 does not have any cousins so its sum is 0.
38+
- Node with value 2 does not have any cousins so its sum is 0.
39+
```
40+
41+
## 结语
42+
43+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
44+
45+
[title]: https://leetcode.com/problems/cousins-in-binary-tree-ii
46+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type TreeNode struct {
4+
Val int
5+
Left, Right *TreeNode
6+
}
7+
8+
func Solution(root *TreeNode) *TreeNode {
9+
if root == nil {
10+
return nil
11+
}
12+
sum := 0
13+
queue := [][2]*TreeNode{{nil, root}}
14+
root.Val = 0
15+
for len(queue) > 0 {
16+
nq := make([][2]*TreeNode, 0)
17+
sum = 0
18+
del := make(map[*TreeNode]int)
19+
for _, item := range queue {
20+
if item[1].Left != nil {
21+
nq = append(nq, [2]*TreeNode{item[1], item[1].Left})
22+
sum += item[1].Left.Val
23+
del[item[1]] += item[1].Left.Val
24+
}
25+
if item[1].Right != nil {
26+
nq = append(nq, [2]*TreeNode{item[1], item[1].Right})
27+
sum += item[1].Right.Val
28+
del[item[1]] += item[1].Right.Val
29+
}
30+
}
31+
for _, item := range nq {
32+
item[1].Val = sum - del[item[0]]
33+
}
34+
queue = nq
35+
}
36+
return root
537
}

leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/Solution_test.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,41 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs *TreeNode
14+
expect *TreeNode
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", &TreeNode{
17+
Val: 5,
18+
Left: &TreeNode{
19+
Val: 4,
20+
Left: &TreeNode{Val: 1},
21+
Right: &TreeNode{Val: 10},
22+
},
23+
Right: &TreeNode{
24+
Val: 9,
25+
Right: &TreeNode{Val: 7},
26+
},
27+
}, &TreeNode{
28+
Val: 0,
29+
Left: &TreeNode{
30+
Val: 0,
31+
Left: &TreeNode{Val: 7},
32+
Right: &TreeNode{Val: 7},
33+
},
34+
Right: &TreeNode{
35+
Val: 0,
36+
Right: &TreeNode{Val: 11},
37+
},
38+
}},
39+
{"TestCase1", &TreeNode{
40+
Val: 3,
41+
Left: &TreeNode{Val: 1},
42+
Right: &TreeNode{Val: 2},
43+
}, &TreeNode{
44+
Val: 0,
45+
Left: &TreeNode{Val: 0},
46+
Right: &TreeNode{Val: 0},
47+
}},
1948
}
2049

2150
// 开始测试
@@ -30,10 +59,10 @@ func TestSolution(t *testing.T) {
3059
}
3160
}
3261

33-
// 压力测试
62+
// 压力测试
3463
func BenchmarkSolution(b *testing.B) {
3564
}
3665

37-
// 使用案列
66+
// 使用案列
3867
func ExampleSolution() {
3968
}
Loading
Loading

0 commit comments

Comments
 (0)