617. 合并二叉树-TS版【简单题】

题目链接

function mergeTrees(root1: TreeNode | null, root2: TreeNode | null): TreeNode | null {
    if (!root1) return root2;
    if (!root2) return root1;
    const newVal = root1.val + root2.val;
    const leftChild = mergeTrees(root1.left, root2.left);
    const rightChild = mergeTrees(root1.right, root2.right);
    return new TreeNode(newVal, leftChild, rightChild);
};

results matching ""

    No results matching ""