LeetCode 算法題 – Merge Two Binary Trees

本頁內容

合併兩個二叉樹,如果結點重疊,則該結點值為兩者對應結點之和。否則,將當前對應的非空結點作為當前結點。

原題

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.

You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.

Example 1:

Input:

1        Tree 1                     Tree 2                  
2              1                         2                             
3             / \                       / \                            
4            3   2                     1   3                        
5           /                           \   \                      
6          5                             4   7                  

Output:

1            Merged tree:
2             3
3            / \
4           4   5
5          / \   \ 
6    
7         5   4   7

Note: The merging process must start from the root nodes of both trees.

分析

可以使用遞歸實現:

  • 如果其中一棵樹為 null,則直接返回另外一棵樹
  • 如果都是非 null 的二叉樹,則將該結點的值更新為兩者值之和,並向下遞歸,合併兩者的左右結點

實現

 1func mergeTrees(t1 *TreeNode, t2 *TreeNode) *TreeNode {
 2    if t1 == nil {
 3        return t2
 4    } else if t2 == nil {
 5        return t1
 6    }
 7
 8    return &TreeNode{
 9        Val:   t1.Val + t2.Val,
10        Left:  mergeTrees(t1.Left, t2.Left),
11        Right: mergeTrees(t1.Right, t2.Right),
12    }
13}
razonyang
2024年11月5日 星期二 2017年7月2日 星期日