LeetCode | 1367. Linked List in Binary Tree二叉树中的列表【Python】

落日映苍穹つ 2023-07-10 08:27 96阅读 0赞

LeetCode 1367. Linked List in Binary Tree二叉树中的列表【Medium】【Python】【DFS】

Problem

LeetCode

Given a binary tree root and a linked list with head as the first node.

Return True if all the elements in the linked list starting from the head correspond to some downward path connected in the binary tree otherwise return False.

In this context downward path means a path that starts at some node and goes downwards.

Example 1:

format_png

  1. Input: head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
  2. Output: true
  3. Explanation: Nodes in blue form a subpath in the binary Tree.

Example 2:

format_png 1

  1. Input: head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
  2. Output: true

Example 3:

  1. Input: head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
  2. Output: false
  3. Explanation: There is no path in the binary tree that contains all the elements of the linked list from head.

Constraints:

  • 1 <= node.val <= 100 for each node in the linked list and binary tree.
  • The given linked list will contain between 1 and 100 nodes.
  • The given binary tree will contain between 1 and 2500 nodes.

问题

力扣

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

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

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

示例 1:

format_png

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

示例 2:

format_png 1

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

示例 3:

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

提示:

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

思路

两重DFS

  1. 第一重:找到起点。先判断当前节点,如果不对就判断左子树和右子树。
  2. 第二重:从找到的起点开始判断剩下的点。
Python3代码
  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6. # Definition for a binary tree node.
  7. # class TreeNode:
  8. # def __init__(self, x):
  9. # self.val = x
  10. # self.left = None
  11. # self.right = None
  12. class Solution:
  13. def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
  14. if head == None:
  15. return True
  16. if root == None:
  17. return False
  18. # judge root, then judge root.left and root.right
  19. return self.isSub(head, root) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right)
  20. def isSub(self, head, node):
  21. # list is over
  22. if head == None:
  23. return True
  24. # list is not over and tree is over
  25. if node == None:
  26. return False
  27. # not equal
  28. if not head.val == node.val:
  29. return False
  30. # equal, then left and right
  31. return self.isSub(head.next, node.left) or self.isSub(head.next, node.right)

代码地址

GitHub链接

参考

Jerry学长题解

发表评论

表情:
评论列表 (有 0 条评论,96人围观)

还没有评论,来说两句吧...

相关阅读