用递归来做:

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def isSame(self,root1,root2):
        if root2==None:
            return True
        if root1==None:
            return False
        
        if root1.val!=root2.val:
            return False
#         left,right=True,True
#         if(root1.left):
#             left=self.isSame(root1.left, root2.left)
        
#         if root1.right:
#             right=self.isSame(root1.right, root2.right)
#         flag=left and right
        flag=(self.isSame(root1.left, root2.left)) and (self.isSame(root1.right, root2.right))
        return flag
        
    def HasSubtree(self, pRoot1, pRoot2):
        # write code here
        if pRoot1==None&nbs***bsp;pRoot2==None:
            return False
        
        if self.isSame(pRoot1,pRoot2):
            return True
        if self.HasSubtree(pRoot1.left, pRoot2)&nbs***bsp;self.HasSubtree(pRoot1.right, pRoot2):
            return True
#         return False