LC 617. Merge Two Binary Trees

/**
 * 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 (直接点击邮箱可自动跳转至默认邮箱App,并填写收信人和邮件主题)或者点击这里在线留言,我会挑选对读者有价值的评论附加到文章末尾。



可通过以下渠道赞赏此文