diff --git a/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/README.md b/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/README.md new file mode 100644 index 000000000..663c673aa --- /dev/null +++ b/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/README.md @@ -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 diff --git a/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/Solution.go b/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/Solution.go index d115ccf5e..cd9daa9d0 100755 --- a/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/Solution.go +++ b/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/Solution.go @@ -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 } diff --git a/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/Solution_test.go b/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/Solution_test.go index 14ff50eb4..f014dc8ae 100755 --- a/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/Solution_test.go +++ b/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/Solution_test.go @@ -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}, + }}, } // 开始测试 @@ -30,10 +59,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { } diff --git a/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/diagram33.png b/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/diagram33.png new file mode 100644 index 000000000..5401d3e75 Binary files /dev/null and b/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/diagram33.png differ diff --git a/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/example11.png b/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/example11.png new file mode 100644 index 000000000..135e56bd2 Binary files /dev/null and b/leetcode/2601-2700/2641.Cousins-in-Binary-Tree-II/example11.png differ