Content-Length: 256370 | pFad | https://github.com/Geekhyt/javascript-leetcode/issues/87

6F 617. 合并二叉树 · Issue #87 · Geekhyt/javascript-leetcode · GitHub
Skip to content

617. 合并二叉树 #87

@Geekhyt

Description

@Geekhyt

原题链接

dfs 递归

在 root1 上直接修改,将两个树对应的节点相加后,赋值给 root1,然后递归执行两个左右子树。

const mergeTrees = function(root1, root2) {
    const preOrder = function(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)
}
  • 时间复杂度: O(min(m,n))
  • 空间复杂度: O(min(m,n))

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: https://github.com/Geekhyt/javascript-leetcode/issues/87

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy