Skip to content

Commit

Permalink
Merge pull request #853 from 0xff-dev/2641
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 2641
  • Loading branch information
6boris committed Jun 6, 2024
2 parents ec47cff + 1dc23dd commit ffe3acc
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 9 deletions.
46 changes: 46 additions & 0 deletions leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# [1001.Grid Illumination][title]

## Description
Given the `root` of a binary tree, replace the value of each node in the tree with the **sum of all its cousins' values**.

Two nodes of a binary tree are **cousins** if they have the same depth with different parents.

Return the `root` of the modified tree.

**Note** that the depth of a node is the number of edges in the path from the root node to it.

**Example 1:**

![1](./example11.png)

```
Input: root = [5,4,9,1,10,null,7]
Output: [0,0,0,7,7,null,11]
Explanation: The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
- Node with value 5 does not have any cousins so its sum is 0.
- Node with value 4 does not have any cousins so its sum is 0.
- Node with value 9 does not have any cousins so its sum is 0.
- Node with value 1 has a cousin with value 7 so its sum is 7.
- Node with value 10 has a cousin with value 7 so its sum is 7.
- Node with value 7 has cousins with values 1 and 10 so its sum is 11.
```

**Example 2:**

![2](./diagram33.png)

```
Input: root = [3,1,2]
Output: [0,0,0]
Explanation: The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
- Node with value 3 does not have any cousins so its sum is 0.
- Node with value 1 does not have any cousins so its sum is 0.
- Node with value 2 does not have any cousins so its sum is 0.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/cousins-in-binary-tree-ii
[me]: https://github.com/kylesliu/awesome-golang-algorithm
36 changes: 34 additions & 2 deletions leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
package Solution

func Solution(x bool) bool {
return x
type TreeNode struct {
Val int
Left, Right *TreeNode
}

func Solution(root *TreeNode) *TreeNode {
if root == nil {
return nil
}
sum := 0
queue := [][2]*TreeNode{{nil, root}}
root.Val = 0
for len(queue) > 0 {
nq := make([][2]*TreeNode, 0)
sum = 0
del := make(map[*TreeNode]int)
for _, item := range queue {
if item[1].Left != nil {
nq = append(nq, [2]*TreeNode{item[1], item[1].Left})
sum += item[1].Left.Val
del[item[1]] += item[1].Left.Val
}
if item[1].Right != nil {
nq = append(nq, [2]*TreeNode{item[1], item[1].Right})
sum += item[1].Right.Val
del[item[1]] += item[1].Right.Val
}
}
for _, item := range nq {
item[1].Val = sum - del[item[0]]
}
queue = nq
}
return root
}
43 changes: 36 additions & 7 deletions leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,41 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs *TreeNode
expect *TreeNode
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", &TreeNode{
Val: 5,
Left: &TreeNode{
Val: 4,
Left: &TreeNode{Val: 1},
Right: &TreeNode{Val: 10},
},
Right: &TreeNode{
Val: 9,
Right: &TreeNode{Val: 7},
},
}, &TreeNode{
Val: 0,
Left: &TreeNode{
Val: 0,
Left: &TreeNode{Val: 7},
Right: &TreeNode{Val: 7},
},
Right: &TreeNode{
Val: 0,
Right: &TreeNode{Val: 11},
},
}},
{"TestCase1", &TreeNode{
Val: 3,
Left: &TreeNode{Val: 1},
Right: &TreeNode{Val: 2},
}, &TreeNode{
Val: 0,
Left: &TreeNode{Val: 0},
Right: &TreeNode{Val: 0},
}},
}

// 开始测试
Expand All @@ -30,10 +59,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ffe3acc

Please sign in to comment.