617. Merge Two Binary Trees

题目地址:https://leetcode.com/problems/merge-two-binary-trees/description/
大意:合并二叉树

# 617. Merge Two Binary Trees
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def mergeTrees(self, t1, t2):
        """
        :type t1: TreeNode
        :type t2: TreeNode
        :rtype: TreeNode
        """
        if t1 is not None and t2 is not None:
            t1.val += t2.val
            t1.left = self.mergeTrees(t1.left,t2.left)
            t1.right = self.mergeTrees(t1.right,t2.right)
        elif t1 is None and t2 is not None:
            t1 = t2
        return t1

t1 = TreeNode(1)
t1.left = TreeNode(3)
t1.right = TreeNode(2)
t1.left.left = TreeNode(5)

t2 = TreeNode(2)
t2.right = TreeNode(3)
t2.right.right = TreeNode(7)
t2.left = TreeNode(1)
t2.left.right = TreeNode(4)

t = Solution().mergeTrees(t1, t2)

所有题目解题方法和答案代码地址:https://github.com/fredfeng0326/LeetCode
    原文作者:fred_33c7
    原文地址: https://www.jianshu.com/p/a8095c98ec64
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞