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 2e4f123

Browse files
committedJan 14, 2020
Add solution #617
1 parent 150bdaf commit 2e4f123

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
 
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 617. Merge Two Binary Trees
3+
* https://leetcode.com/problems/merge-two-binary-trees/
4+
* Difficulty: Easy
5+
*
6+
* Given two binary trees and imagine that when you put one
7+
* of them to cover the other, some nodes of the two trees
8+
* are overlapped while the others are not.
9+
*
10+
* You need to merge them into a new binary tree.
11+
* The merge rule is that if two nodes overlap, then sum
12+
* node values up as the new value of the merged node.
13+
* Otherwise, the NOT null node will be used as the
14+
* node of new tree.
15+
*/
16+
17+
/**
18+
* Definition for a binary tree node.
19+
* function TreeNode(val) {
20+
* this.val = val;
21+
* this.left = this.right = null;
22+
* }
23+
*/
24+
/**
25+
* @param {TreeNode} t1
26+
* @param {TreeNode} t2
27+
* @return {TreeNode}
28+
*/
29+
var mergeTrees = function(t1, t2) {
30+
if (!t1) return t2;
31+
if (!t2) return t1;
32+
33+
t1.val += t2.val;
34+
t1.left = mergeTrees(t1.left, t2.left);
35+
t1.right = mergeTrees(t1.right, t2.right);
36+
37+
return t1;
38+
};

0 commit comments

Comments
 (0)
Please sign in to comment.