题解 | #相同的二叉树#

相同的二叉树

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

全部评论

相关推荐

06-23 11:43
门头沟学院 Java
allin校招的烤冷...:我靠,今天中午我也是这个hr隔一个星期发消息给我。问的问题还是一模一样的😅
点赞 评论 收藏
分享
但听说转正率很低,我现在有在实习了,好纠结要不要去
熬夜脱发码农:转正率低归低,但是实习的经历你可以拿着,又不是说秋招不准备了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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