572. 另一个树的子树

给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树。

示例 1:
给定的树 s:

3
/
4 5
/
1 2
给定的树 t:

4
/
1 2
返回 true,因为 t 与 s 的一个子树拥有相同的结构和节点值。

示例 2:
给定的树 s:

3
/
4 5
/
1 2
/
0
给定的树 t:

4
/
1 2
返回 false。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/subtree-of-another-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:
        def isIdentical(s,t):
            if not s and not t:return True
            return s and t and s.val==t.val
                     and isIdentical(s.left,t.left) and isIdentical(s.right,t.right)
        if not s and not t:return True
        if not s and t:return False
        return isIdentical(s,t) or self.isSubtree(s.left,t) or self.isSubtree(s.right,t)
原文地址:https://www.cnblogs.com/xxxsans/p/14040299.html