LC 617. Merge Two Binary Trees

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root1
 * @param {TreeNode} root2
 * @return {TreeNode}
 */
var mergeTrees = function (root1, root2) {
  const preOrder = (root1, root2) => {
    if (!root1) return root2
    if (!root2) return root1
    root1.val += root2.val
    root1.left = preOrder(root1.left, root2.left)
    root1.right = preOrder(root1.right, root2.right)
    return root1
  }
  return preOrder(root1, root2)
};





评论和交流请发送邮件到 me@tianhegao.com





通过以下渠道赞赏此文