Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions leetcode/601-700/0617.Merge-Two-Binary-Trees/README.md
Original file line number Diff line number Diff line change
@@ -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]
```

## 结语

Expand Down
18 changes: 16 additions & 2 deletions leetcode/601-700/0617.Merge-Two-Binary-Trees/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
47 changes: 37 additions & 10 deletions leetcode/601-700/0617.Merge-Two-Binary-Trees/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading