Leetcode 572. Subtree of Another Tree
题目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3fb72e8704e07e420a43e6cd0197f1aa.png)
解法:双层递归
虽然这道题目标的easy,但是其实蛮考对于递归的理解的。最直观的解法是双层递归,外层递归遍历节点,内层递归判断当前遍历的节点形成的子树是否与目标树相同。外层循环只要有一个节点符合情况即可,二内层循环需要每个节点都相同才行
python:
class Solution:
def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:
def isSame(t1,t2):
if not t1 and not t2:
return True
if not t1 or