两次递归

树的子结构

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

class Solution:
    def HasSubtree(self, s, t):
        def sametree(p,q):
            if p is None and q is None:
                return True
            elif p is not None and q is not None and p.val==q.val:
                return sametree(p.left,q.left) and sametree(p.right,q.right)
            elif p is not None and q is None:
                return True
            else:
                return False
        def subtree(s,t):
            if s is None or t is None:return False
            if sametree(s,t):
                return True
            return subtree(s.left,t) or subtree(s.right,t)
        return subtree(s,t)

两次递归

全部评论

相关推荐

07-15 11:35
门头沟学院 Java
心里踏实多了,可以安心准备论文了
看不见我ffgh:牛哇佬,要不要来试一试pdd,部门氛围很好
京东开奖153人在聊
点赞 评论 收藏
分享
点赞 评论 收藏
分享
06-12 16:00
天津大学 Java
牛客30236098...:腾讯坏事做尽,终面挂是最破防的 上次被挂了后我连简历都不刷了
点赞 评论 收藏
分享
评论
4
收藏
分享

创作者周榜

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