题解 | #合并二叉树#

合并二叉树

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

全部评论

相关推荐

07-07 12:47
门头沟学院 Java
码农索隆:竟然还真有卡体检报告的
点赞 评论 收藏
分享
强大的马里奥:不太可能,我校计算机硕士就业率99%
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务