diff --git a/leetcode/601-700/0617.Merge-Two-Binary-Trees/README.md b/leetcode/601-700/0617.Merge-Two-Binary-Trees/README.md index 6a40013fa..8c01cc579 100644 --- a/leetcode/601-700/0617.Merge-Two-Binary-Trees/README.md +++ b/leetcode/601-700/0617.Merge-Two-Binary-Trees/README.md @@ -1,28 +1,29 @@ # [617.Merge Two Binary Trees][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given two binary trees `root1` and `root2`. -**Example 1:** +Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree. -``` -Input: a = "11", b = "1" -Output: "100" -``` +Return the merged tree. + +**Note**: The merging process must start from the root nodes of both trees. -## 题意 -> ... +**Example 1:** -## 题解 +![1](./merge.jpg) -### 思路1 -> ... -Merge Two Binary Trees -```go ``` +Input: root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7] +Output: [3,4,5,5,4,null,7] +``` + +**Example 2:** +``` +Input: root1 = [1], root2 = [1,2] +Output: [2,2] +``` ## 结语 diff --git a/leetcode/601-700/0617.Merge-Two-Binary-Trees/Solution.go b/leetcode/601-700/0617.Merge-Two-Binary-Trees/Solution.go index d115ccf5e..c8b50409b 100644 --- a/leetcode/601-700/0617.Merge-Two-Binary-Trees/Solution.go +++ b/leetcode/601-700/0617.Merge-Two-Binary-Trees/Solution.go @@ -1,5 +1,19 @@ package Solution -func Solution(x bool) bool { - return x +type TreeNode struct { + Val int + Left, Right *TreeNode +} + +func Solution(root1 *TreeNode, root2 *TreeNode) *TreeNode { + if root1 == nil { + return root2 + } + if root2 == nil { + return root1 + } + r := &TreeNode{Val: root1.Val + root2.Val} + r.Left = Solution(root1.Left, root2.Left) + r.Right = Solution(root1.Right, root2.Right) + return r } diff --git a/leetcode/601-700/0617.Merge-Two-Binary-Trees/Solution_test.go b/leetcode/601-700/0617.Merge-Two-Binary-Trees/Solution_test.go index 14ff50eb4..63dd57b5d 100644 --- a/leetcode/601-700/0617.Merge-Two-Binary-Trees/Solution_test.go +++ b/leetcode/601-700/0617.Merge-Two-Binary-Trees/Solution_test.go @@ -10,30 +10,57 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + r1, r2 *TreeNode + expect *TreeNode }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", &TreeNode{ + Val: 1, + Left: &TreeNode{ + Val: 3, + Left: &TreeNode{Val: 5}, + }, + Right: &TreeNode{Val: 2}, + }, &TreeNode{ + Val: 2, + Left: &TreeNode{ + Val: 1, + Right: &TreeNode{Val: 4}, + }, + Right: &TreeNode{ + Val: 3, + Right: &TreeNode{Val: 7}, + }, + }, &TreeNode{ + Val: 3, + Left: &TreeNode{ + Val: 4, + Left: &TreeNode{Val: 5}, + Right: &TreeNode{Val: 4}, + }, + Right: &TreeNode{ + Val: 5, + Right: &TreeNode{Val: 7}, + }, + }}, + {"TestCase2", &TreeNode{Val: 1}, &TreeNode{Val: 1, Left: &TreeNode{Val: 2}}, &TreeNode{Val: 2, Left: &TreeNode{Val: 2}}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.r1, c.r2) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.r1, c.r2) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { } diff --git a/leetcode/601-700/0617.Merge-Two-Binary-Trees/merge.jpg b/leetcode/601-700/0617.Merge-Two-Binary-Trees/merge.jpg new file mode 100644 index 000000000..e5d33535a Binary files /dev/null and b/leetcode/601-700/0617.Merge-Two-Binary-Trees/merge.jpg differ