树的子结构

树的子结构

http://www.nowcoder.com/questionTerminal/6e196c44c7004d15b1610b9afca8bd88

class Solution:
    def HasSubtree(self, a, b):
        # write code here
        if not b or not a:
            return False

        return self.contains(a, b) or self.HasSubtree(a.left, b) or self.HasSubtree(a.right, b)

    def contains(self, a, b):
        if not b:
            return True
        if not a:
            return False

        return a.val == b.val and self.contains(a.left, b.left) and self.contains(a.right, b.right)
全部评论

相关推荐

不愿透露姓名的神秘牛友
03-10 14:27
已编辑
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务