-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #853 from 0xff-dev/2641
Add solution and test-cases for problem 2641
- Loading branch information
Showing
5 changed files
with
116 additions
and
9 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.