题解 | #合并二叉树#

合并二叉树

https://www.nowcoder.com/practice/7298353c24cc42e3bd5f0e0bd3d1d759

# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param t1 TreeNode类 
# @param t2 TreeNode类 
# @return TreeNode类
#
import queue
class Solution:
    def mergeTrees(self , t1: TreeNode, t2: TreeNode) -> TreeNode:
        if not t1 and not t2:
            return t1
        if not t1 and t2:
            t1 = t2
        elif t1 and not t2:
            return t1
        else:
            t1.val += t2.val
            if not t1.left:
                t1.left = t2.left
            else:
                self.mergeTrees(t1.left,t2.left)
            if not t1.right:
                t1.right = t2.right
            else:
                self.mergeTrees(t1.right,t2.right)
        return t1

全部评论

相关推荐

喜欢吃蛋糕仰泳鲈鱼是我的神:字节可以找个hr 给你挂了,再放池子捞
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务