美文网首页
判断两棵树是否相同

判断两棵树是否相同

作者: 而立之年的技术控 | 来源:发表于2019-12-21 18:37 被阅读0次
    微信图片_20191221183656.jpg
    class Solution:
        def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
            if p is None and q is None:
                return True
            if p is None:
                return False
            if q is None:
                return False
            
            if p.val != q.val:
                return False
            else:
                left = self.isSameTree(p.left, q.left)
                right = self.isSameTree(p.right, q.right)
                return left and right
    

    相关文章

      网友评论

          本文标题:判断两棵树是否相同

          本文链接:https://www.haomeiwen.com/subject/mtggnctx.html