Easy草稿★★★★★O(min(m, n)) 時間 · O(min(h1, h2)) 空間
TreeDFSRecursion
Patterns🌳 樹形 DFS
尚未複習過

解法已隱藏 — 先讀題目敘述、自己想想看,再點上方按鈕揭曉。

617Merge Two Binary TreesTreesEasyTrees

給定兩棵二元樹,將它們合併成一棵新樹。合併規則:若兩個節點重疊,則將它們的值相加;若只有一方有節點,則使用該節點。

Example:

Input: root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7] Output: [3,4,5,5,4,null,7]

Intuition

TIP

同時遍歷兩棵樹,對應位置的節點值相加,遞迴合併左右子樹。

Approaches

⭐ 1. Recursive DFS — O(min(m, n)) / O(min(h1, h2))
  • Idea: 同時遞迴兩棵樹,合併對應節點
  • Time: O(min(m, n)),m、n 為兩棵樹的節點數,只遍歷重疊部分
  • Space: O(min(h1, h2)),遞迴深度取決於較矮的樹
class Solution {
    fun mergeTrees(root1: TreeNode?, root2: TreeNode?): TreeNode? {
        if (root1 == null) return root2
        if (root2 == null) return root1
        val merged = TreeNode(root1.`val` + root2.`val`)
        merged.left = mergeTrees(root1.left, root2.left)
        merged.right = mergeTrees(root1.right, root2.right)
        return merged
    }
}
2. Iterative BFS — O(min(m, n)) / O(min(m, n))
  • Idea: 使用佇列同時遍歷兩棵樹,直接修改第一棵樹
  • Time: O(min(m, n))
  • Space: O(min(m, n))
class Solution {
    fun mergeTrees(root1: TreeNode?, root2: TreeNode?): TreeNode? {
        if (root1 == null) return root2
        if (root2 == null) return root1
        val queue = ArrayDeque<Pair<TreeNode, TreeNode>>()
        queue.add(root1 to root2)
        while (queue.isNotEmpty()) {
            val (n1, n2) = queue.removeFirst()
            n1.`val` += n2.`val`
            if (n1.left != null && n2.left != null) {
                queue.add(n1.left!! to n2.left!!)
            } else if (n1.left == null) {
                n1.left = n2.left
            }
            if (n1.right != null && n2.right != null) {
                queue.add(n1.right!! to n2.right!!)
            } else if (n1.right == null) {
                n1.right = n2.right
            }
        }
        return root1
    }
}

🔑 Takeaways