Tianhe Gao

LC 617. Merge Two Binary Trees

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

No notes link to this note

Welcome to tell me your thoughts via "email"
UP