Skip to content

Commit 1cee2e7

Browse files
authored
Merge pull request #1025 from 0xff-dev/617
Add solution and test-cases for problem 617
2 parents 7f85814 + 5e3c634 commit 1cee2e7

File tree

4 files changed

+69
-27
lines changed

4 files changed

+69
-27
lines changed

leetcode/601-700/0617.Merge-Two-Binary-Trees/README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# [617.Merge Two Binary Trees][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given two binary trees `root1` and `root2`.
75

8-
**Example 1:**
6+
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.
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
Return the merged tree.
9+
10+
**Note**: The merging process must start from the root nodes of both trees.
1411

15-
## 题意
16-
> ...
12+
**Example 1:**
1713

18-
## 题解
14+
![1](./merge.jpg)
1915

20-
### 思路1
21-
> ...
22-
Merge Two Binary Trees
23-
```go
2416
```
17+
Input: root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
18+
Output: [3,4,5,5,4,null,7]
19+
```
20+
21+
**Example 2:**
2522

23+
```
24+
Input: root1 = [1], root2 = [1,2]
25+
Output: [2,2]
26+
```
2627

2728
## 结语
2829

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
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(root1 *TreeNode, root2 *TreeNode) *TreeNode {
9+
if root1 == nil {
10+
return root2
11+
}
12+
if root2 == nil {
13+
return root1
14+
}
15+
r := &TreeNode{Val: root1.Val + root2.Val}
16+
r.Left = Solution(root1.Left, root2.Left)
17+
r.Right = Solution(root1.Right, root2.Right)
18+
return r
519
}

leetcode/601-700/0617.Merge-Two-Binary-Trees/Solution_test.go

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

2148
// 开始测试
2249
for i, c := range cases {
2350
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
51+
got := Solution(c.r1, c.r2)
2552
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
53+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
54+
c.expect, got, c.r1, c.r2)
2855
}
2956
})
3057
}
3158
}
3259

33-
// 压力测试
60+
// 压力测试
3461
func BenchmarkSolution(b *testing.B) {
3562
}
3663

37-
// 使用案列
64+
// 使用案列
3865
func ExampleSolution() {
3966
}
33.9 KB
Loading

0 commit comments

Comments
 (0)