给你一棵以 root
为根的二叉树和一个 head
为第一个节点的链表。
如果在二叉树中,存在一条一直向下的路径,且每个点的数值恰好一一对应以 head
为首的链表中每个节点的值,那么请你返回 True
,否则返回 False
。
一直向下的路径的意思是:从树中某个节点开始,一直连续向下的路径。
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next # 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 dfs(self, head: ListNode, rt: TreeNode) -> bool: if not head: return True if not rt: return False if rt.val != head.val: return False return self.dfs(head.next, rt.left) or self.dfs(head.next, rt.right) def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool: if not root: return False return self.dfs(head, root) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right)
标签:head,right,return,val,self,链表,1367,二叉树,root From: https://www.cnblogs.com/xxlm/p/18642638