Leetcode572. 另一个树的子树——python求解

题目:
给定两个非空二叉树 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

python字符串匹配:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:
            return str(t) in str(s)

猜你喜欢

转载自blog.csdn.net/weixin_41729258/article/details/105965742
今日推荐