题解 | #合并二叉树#

合并二叉树

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

全部评论

相关推荐

在评审的大师兄很完美:像这种一般就是部门不匹配 转移至其他部门然后挂掉 我就是这样被挂了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务