Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

617. 合并二叉树 #87

Open
Geekhyt opened this issue Sep 20, 2021 · 0 comments
Open

617. 合并二叉树 #87

Geekhyt opened this issue Sep 20, 2021 · 0 comments
Labels

Comments

@Geekhyt
Copy link
Owner

Geekhyt commented Sep 20, 2021

原题链接

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))
@Geekhyt Geekhyt added the 简单 label Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant