File tree 1 file changed +38
-0
lines changed
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments