呓语Beta 2.0 首页 镜头后 灶台前 捐赠者名单 有趣的小站

leetcode 572 Subtree Of Another Tree

文档信息

leetcode 572 Subtree Of Another Tree

解法一

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def isSubtree(self, s, t):
        """
        :type s: TreeNode
        :type t: TreeNode
        :rtype: bool
        """
        
        def _isSameTree(s, t):
            if s and t:
                return s.val == t.val and _isSameTree(s.left, t.left) and _isSameTree(s.right, t.right)
            else:
                return s == t
        
        if s and t:
            if _isSameTree(s, t):
                return True
            else:
                return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)
        else:
            return s == t

note

捐赠支持

可以使用支付宝转帐给richard.ma.19850509#gmail.com(请将#替换为@),谢谢!

Fork me on GitHub