forked from azl397985856/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
luzhipeng
committed
Jun 11, 2019
1 parent
14243fc
commit fac46b8
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* @lc app=leetcode id=617 lang=javascript | ||
* | ||
* [617] Merge Two Binary Trees | ||
*/ | ||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val) { | ||
* this.val = val; | ||
* this.left = this.right = null; | ||
* } | ||
*/ | ||
/** | ||
* @param {TreeNode} t1 | ||
* @param {TreeNode} t2 | ||
* @return {TreeNode} | ||
*/ | ||
var mergeTrees = function(t1, t2) { | ||
// 递归,由于树是一种递归的数据结构,因此递归是符合直觉且比较简单的 | ||
if (t1 === null) return t2; | ||
if (t2 === null) return t1; | ||
t1.val += t2.val; | ||
t1.left = mergeTrees(t1.left, t2.left); | ||
t1.right = mergeTrees(t1.right, t2.right); | ||
return t1; | ||
}; |