树的子结构

树的子结构

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)
全部评论

相关推荐

冲鸭2024:亚信不去也罢
投递亚信科技(中国)有限公司等公司6个岗位
点赞 评论 收藏
分享
10-28 17:30
已编辑
华东交通大学 Java
想进开水团喝开水:字节的hr的本职工作就是黄金矿工
秋招笔试记录
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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