【树-中等】1367. 二叉树中的列表

题目
给你一棵以 root 为根的二叉树和一个 head 为第一个节点的链表。

如果在二叉树中,存在一条一直向下的路径,且每个点的数值恰好一一对应以 head 为首的链表中每个节点的值,那么请你返回 True ,否则返回 False 。

一直向下的路径的意思是:从树中某个节点开始,一直连续向下的路径。

示例 1:

输入:head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
输出:true
解释:树中蓝色的节点构成了与链表对应的子路径。

示例 2:

输入:head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
输出:true

示例 3:

输入:head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
输出:false
解释:二叉树中不存在一一对应链表的路径。

提示:

二叉树和链表中的每个节点的值都满足 1 <= node.val <= 100 。
链表包含的节点数目在 1 到 100 之间。
二叉树包含的节点数目在 1 到 2500 之间。

【代码】
在这里插入图片描述

class Solution:
    def dfs(self,root):
        if not root:
            return         
        self.path.append(str(root.val))
        if not root.left and not root.right:
            if self.head in "#".join(self.path):
                self.flag=True
        self.dfs(root.left)
        self.dfs(root.right)
        self.path.pop()

    def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
        self.path=[]
        self.head=[]
        self.flag=False
        while head:
            self.head.append(str(head.val))
            head=head.next
        self.head="#".join(self.head)
        self.dfs(root)
        return self.flag

【方法2】
在这里插入图片描述

class Solution:
    def dfs(self,head,root):
        if not head:
            return True
        if not root:
            return False
        if head.val!=root.val:
            return False
        return self.dfs(head.next,root.left) or self.dfs(head.next,root.right)
    def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
        if not root:
            return False
        return self.dfs(head,root) or self.isSubPath(head,root.left) or self.isSubPath(head,root.right)

Guess you like

Origin blog.csdn.net/kz_java/article/details/119853829