Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6b14ded

Browse files
committedMay 3, 2020
add 617. Merge Two Binary Trees 🍺
1 parent 9a805cb commit 6b14ded

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ My LeetCode solutions with Chinese explanation. 我的LeetCode中文题解。
321321
| 572 |[Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/)|[C++](solutions/572.%20Subtree%20of%20Another%20Tree.md)|Easy| |
322322
| 581 |[Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/581.%20Shortest%20Unsorted%20Continuous%20Subarray.md)|Easy| |
323323
| 605 |[Can Place Flowers](https://leetcode.com/problems/can-place-flowers)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/605.%20Can%20Place%20Flowers.md)|Easy| |
324+
| 617 |[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)|[C++](solutions/617.%20Merge%20Two%20Binary%20Trees.md)|Easy| |
324325
| 628 |[Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/628.%20Maximum%20Product%20of%20Three%20Numbers.md)|Easy| |
325326
| 643 |[Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/643.%20Maximum%20Average%20Subarray%20I.md)|Easy| |
326327
| 661 |[Image Smoother](https://leetcode.com/problems/image-smoother)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/661.%20Image%20Smoother.md)|Easy| |
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# [617. Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)
2+
3+
# 思路
4+
5+
合并两个二叉树。就是一个很简单的递归:
6+
*`t1``t2`任意一个为空,那么直接返回另一个即可(递归出口);
7+
* 否则,将`t1`的值加上`t2`的值,然后递归合并`t1``t2`的左子树以及`t1``t2`的右子树。
8+
9+
# C++
10+
``` C++
11+
class Solution {
12+
public:
13+
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
14+
if(t1 == NULL) return t2;
15+
if(t2 == NULL) return t1;
16+
17+
t1 -> val += t2 -> val;
18+
t1 -> left = mergeTrees(t1 -> left, t2 -> left);
19+
t1 -> right = mergeTrees(t1 -> right, t2 -> right);
20+
delete t2;
21+
return t1;
22+
}
23+
};
24+
```

0 commit comments

Comments
 (0)
Please sign in to comment.