以前序遍历的方式遍历整棵树,遇到与子树根节点相等的值,再进入子树的前序遍历比较,主要相同条件是主树和子树完全重合即 not root1 and not root2

# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param root1 TreeNode类 
# @param root2 TreeNode类 
# @return bool布尔型
#
class Solution:
    def isContains(self , root1: TreeNode, root2: TreeNode) -> bool:
        # write code here
        if not root2 and not root1:
            return True
        if (not root1 and root2) or (root1 and not root2):
            return False
        if root1.val == root2.val:
            if self.sub_comtains(root1, root2):
                return True
        return self.isContains(root1.left, root2) or self.isContains(root1.right, root2)
         
    
    def sub_comtains(self, root1: TreeNode, root2: TreeNode) ->bool:
        if not root2 and not root1:
            return True
        if (not root1 and root2) or (root1 and not root2):
            return False
        return root1.val == root2.val and self.sub_comtains(root1.left, root2.left) and self.sub_comtains(root1.right, root2.right)