Skip to content

Commit

Permalink
feat: #617
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhipeng committed Jun 11, 2019
1 parent 14243fc commit fac46b8
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 617.merge-two-binary-trees.js
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;
};

0 comments on commit fac46b8

Please sign in to comment.