Skip to content

Commit bc7c912

Browse files
authored
Merge pull request halfrost#155 from brenobaptista/solution-617
Added solution 617
2 parents dbf56b5 + 44431be commit bc7c912

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package leetcode
2+
3+
import (
4+
"github.com/halfrost/LeetCode-Go/structures"
5+
)
6+
7+
// TreeNode define
8+
type TreeNode = structures.TreeNode
9+
10+
/**
11+
* Definition for a binary tree node.
12+
* type TreeNode struct {
13+
* Val int
14+
* Left *TreeNode
15+
* Right *TreeNode
16+
* }
17+
*/
18+
19+
func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
20+
if root1 == nil {
21+
return root2
22+
}
23+
24+
if root2 == nil {
25+
return root1
26+
}
27+
28+
root1.Val += root2.Val
29+
root1.Left = mergeTrees(root1.Left, root2.Left)
30+
root1.Right = mergeTrees(root1.Right, root2.Right)
31+
32+
return root1
33+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/halfrost/LeetCode-Go/structures"
8+
)
9+
10+
type question617 struct {
11+
para617
12+
ans617
13+
}
14+
15+
// para 是参数
16+
// one 代表第一个参数
17+
type para617 struct {
18+
one []int
19+
another []int
20+
}
21+
22+
// ans 是答案
23+
// one 代表第一个答案
24+
type ans617 struct {
25+
one []int
26+
}
27+
28+
func Test_Problem617(t *testing.T) {
29+
30+
qs := []question617{
31+
32+
{
33+
para617{[]int{}, []int{}},
34+
ans617{[]int{}},
35+
},
36+
37+
{
38+
para617{[]int{}, []int{1}},
39+
ans617{[]int{1}},
40+
},
41+
42+
{
43+
para617{[]int{1, 3, 2, 5}, []int{2, 1, 3, structures.NULL, 4, structures.NULL, 7}},
44+
ans617{[]int{3, 4, 5, 5, 4, structures.NULL, 7}},
45+
},
46+
47+
{
48+
para617{[]int{1}, []int{1, 2}},
49+
ans617{[]int{2, 2}},
50+
},
51+
}
52+
53+
fmt.Printf("------------------------Leetcode Problem 617------------------------\n")
54+
55+
for _, q := range qs {
56+
_, p := q.ans617, q.para617
57+
fmt.Printf("【input】:%v ", p)
58+
root1 := structures.Ints2TreeNode(p.one)
59+
root2 := structures.Ints2TreeNode(p.another)
60+
fmt.Printf("【output】:%v \n", mergeTrees(root1, root2))
61+
}
62+
fmt.Printf("\n\n\n")
63+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [617. Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)
2+
3+
## 题目
4+
5+
You are given two binary trees **root1** and **root2**.
6+
7+
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.
8+
9+
Return the merged tree.
10+
11+
**Note**: The merging process must start from the root nodes of both trees.
12+
13+
**Example 1**:
14+
15+
```
16+
Input: root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
17+
Output: [3,4,5,5,4,null,7]
18+
```
19+
20+
**Example 2**:
21+
22+
```
23+
Input: root1 = [1], root2 = [1,2]
24+
Output: [2,2]
25+
```
26+
27+
**Constraints**:
28+
29+
1. The number of nodes in both trees is in the range [0, 2000].
30+
2. -104 <= Node.val <= 104

0 commit comments

Comments
 (0)