题解 | #判断t1树中是否有与t2树完全相同的子树#
判断t1树中是否有与t2树完全相同的子树
http://www.nowcoder.com/practice/4eaccec5ee8f4fe8a4309463b807a542
# 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 , root2 ):
# write code here
if not root1 and not root2:
return True
if not root1 or not root2:
return False
if root1.val == root2.val:
return self.isContains(root1.left, root2.left) and self.isContains(root1.right, root2.right)
return self.isContains(root1.left, root2) or self.isContains(root1.right, root2)