输入两棵二叉树A和B,判断B是不是A的子结构。(约定空树不是任意一个树的子结构)
B是A的子结构, 即 A中有出现和B相同的结构和节点值。
class Solution(object):
def isSubStructure(self, A, B):
"""
:type A: TreeNode
:type B: TreeNode
:rtype: bool
"""
def re(A,B):
if not B:
return True
if not A or A.val!=B.val:
return False
return re(A.left,B.left) and re(A.right,B.right)
return bool(A and B) and (re(A,B) or self.isSubStructure(A.left,B) or self.isSubStructure(A.right,B))