Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 2407f78

Browse files
aQuaaQua
aQua
authored and
aQua
committed
617 added
1 parent dddf3b8 commit 2407f78

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [617. Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)
2+
3+
## 题目
4+
5+
Given two binary trees and 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.
6+
7+
You need to merge them 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 new tree.
8+
9+
Example 1:
10+
11+
```text
12+
Input:
13+
Tree 1 Tree 2
14+
1 2
15+
/ \ / \
16+
3 2 1 3
17+
/ \ \
18+
5 4 7
19+
Output:
20+
Merged tree:
21+
3
22+
/ \
23+
4 5
24+
/ \ \
25+
5 4 7
26+
```
27+
28+
Note: The merging process must start from the root nodes of both trees.
29+
30+
## 解题思路
31+
32+
见程序注释
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package Problem0617
2+
3+
import (
4+
"github.com/aQuaYi/LeetCode-in-Go/kit"
5+
)
6+
7+
type TreeNode = kit.TreeNode
8+
9+
func mergeTrees(t1 *TreeNode, t2 *TreeNode) *TreeNode {
10+
11+
return nil
12+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package Problem0617
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/aQuaYi/LeetCode-in-Go/kit"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
// tcs is testcase slice
13+
var tcs = []struct {
14+
pre1, in1 []int
15+
pre2, in2 []int
16+
ansPost []int
17+
}{
18+
19+
// 可以有多个 testcase
20+
}
21+
22+
func Test_fn(t *testing.T) {
23+
ast := assert.New(t)
24+
25+
for _, tc := range tcs {
26+
fmt.Printf("~~%v~~\n", tc)
27+
t1 := kit.PreIn2Tree(tc.pre1, tc.in1)
28+
t2 := kit.PreIn2Tree(tc.pre2, tc.in2)
29+
ansPost := kit.Tree2Postorder(mergeTrees(t1, t2))
30+
ast.Equal(tc.ansPost, ansPost, "输入:%v", tc)
31+
}
32+
}
33+
34+
func Benchmark_fn(b *testing.B) {
35+
for i := 0; i < b.N; i++ {
36+
for _, tc := range tcs {
37+
t1 := kit.PreIn2Tree(tc.pre1, tc.in1)
38+
t2 := kit.PreIn2Tree(tc.pre2, tc.in2)
39+
mergeTrees(t1, t2)
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)