子树
有两个不同大小的二叉树: T1 有上百万的节点; T2 有好几百的节点。请设计一种算法,判定 T2 是否为 T1的子树。
子树
def isEqual(self,T1, T2):
if T1 == None or T2 == None:
return T1 == T2;
if T1.val != T2.val:
return False
return self.isEqual(T1.right, T2.right) and self.isEqual(T1.left, T2.left)
def isSubtree(self, T1, T2):
# write your code here
if T2 == None:
return True
if T1 == None:
return False
if self.isEqual(T1, T2):
return True
if self.isSubtree(T1.right, T2) or self.isSubtree(T1.left, T2):
return True
return False
网友评论