leetcode-572 另一个树的子树

https://leetcode-cn.com/problems/subtree-of-another-tree/solution/xiong-mao-shua-ti-python3-di-gui-pan-duan-liang-ke/

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def equal(self, a:TreeNode, b:TreeNode):
        if not a and not b:
            return True
        if a is None or b is None:
            return False
        return a.val == b.val and self.equal(a.left, b.left) and self.equal(a.right, b.right)
    def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:
        if not s:
            return False
        if self.equal(s, t):
            return True
        return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)

你可能感兴趣的:(leetcode-572 另一个树的子树)