题解 | #合并二叉树#

合并二叉树

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类
#
class Solution:
    def search(self, this1, this2):
        if this1 is not None and this2 is not None:
            this1.val = this1.val + this2.val
            if this1.left is not None and this2.left is not None:
                self.search(this1.left,this2.left)
            elif this1.left is None and this2.left is not None:
                this1.left = this2.left
            else:
                pass
            
            if this1.right is not None and this2.right is not None:
                self.search(this1.right,this2.right)
            elif this1.right is None and this2.right is not None:
                this1.right = this2.right
            else:
                pass


    def mergeTrees(self , t1: TreeNode, t2: TreeNode) -> TreeNode:
        # write code here

        self.search(t1,t2)

        return t1

全部评论

相关推荐

微风不断:兄弟,你把四旋翼都做出来了那个挺难的吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务