题解 | #相同的二叉树#

相同的二叉树

https://www.nowcoder.com/practice/5a3b2cf4211249c89d6ced7987aeb775?tpId=196&tqId=40271&rp=1&ru=/exam/oj&qru=/exam/oj&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26pageSize%3D50%26search%3D%25E6%25A0%2591%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D196&difficulty=undefined&judgeStatus=undefined&tags=&title=%E6%A0%91

# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param root1 TreeNode类 
# @param root2 TreeNode类 
# @return bool布尔型
#
class Solution:
    def isSameTree(self , root1: TreeNode, root2: TreeNode) -> bool:
        # write code here
        if not root1 and not root2:
            return True
        

        if (not root1 and root2 ) or  (not root2 and root1):
            return False

        if root1.val == root2.val:
            return self.isSameTree(root1.left,root2.left) and  self.isSameTree(root1.right ,root2.right)
        else:
            return False

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务