合并两个二叉树 原

给定两个二叉树,想象当你将它们中的一个覆蓋到另一个上时,两个二叉树的一些节点便会重叠。

你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。

输入: 
	Tree 1                     Tree 2                  
          1                         2                             
         / \                       / \                            
        3   2                     1   3                        
       /                           \   \                      
      5                             4   7                  
输出: 
合并后的树:
	     3
	    / \
	   4   5
	  / \   \ 
	 5   4   7

有很多解法能处理这个问题:主要有递归,迭代,DFS,BFS

首先想到的就是利用树的前序,中序,后序遍历处理

递归

前序遍历解法

class Solution(object):
    def mergeTrees(self, t1, t2):
        if t1 is None and t2 is None:
            return
        if t1 is None:
            return t2
        if t2 is None:
            return t1
        t = TreeNode(t1.val + t2.val)
        t.left = self.mergeTrees(t1.left, t2.left)
        t.right = self.mergeTrees(t1.right, t2.right)
        return t

中序遍历解法

class Solution(object):
    def mergeTrees(self, t1, t2):
        if t1 is None and t2 is None:
            return
        if t1 is None:
            return t2
        if t2 is None:
            return t1
        t_left = self.mergeTrees(t1.left, t2.left)
        t = TreeNode(t1.val + t2.val)
        t.left = t_left
        t.right = self.mergeTrees(t1.right, t2.right)
        return t

后序遍历解法

class Solution(object):
    def mergeTrees(self, t1, t2):
        if t1 is None and t2 is None:
            return
        if t1 is None:
            return t2
        if t2 is None:
            return t1
        t_left = self.mergeTrees(t1.left, t2.left)
        t_right = self.mergeTrees(t1.right, t2.right)
        t = TreeNode(t1.val + t2.val)
        t.left = t_left
        t_right = t_right
        return t

迭代,BFS

class Solution(object):
    def mergeTrees(self, t1, t2):
        """
        迭代合并
        :param t1:
        :param t2:
        :return:
        """
        if t1 is None and t2 is None:
            return None
        if t1 is None:
            return t2
        if t2 is None:
            return t1
        t = TreeNode(t1.val + t2.val)
        q1 = [t1]  # 存第一个要合并的节点
        q2 = [t2]  # 存第二个要合并的节点
        q = [t]  # 存新节点
        while len(q) > 0:
            node = q.pop(0)
            t1 = q1.pop(0)
            t2 = q2.pop(0)
            
            if t1.left is None and t2.left is None:
                pass
            elif t1.left is None:
                node.left = t2.left
            elif t2.left is None:
                node.left = t1.left
            else:
                node.left = TreeNode(t1.left.val + t2.left.val)
                q.append(node.left)
                q1.append(t1.left)
                q2.append(t2.left)
                
            if t1.right is None and t2.right is None:
                pass
            elif t1.right is None:
                node.right = t2.right
            elif t2.right is None:
                node.right = t1.right
            else:
                node.right = TreeNode(t1.right.val + t2.right.val)
                q.append(node.right)
                q1.append(t1.right)
                q2.append(t2.right)
                
        return t

 

点赞