LeetCode Contest 178-1367. 二叉树中的列表 Linked List in Binary Tree

简介: LeetCode Contest 178-1367. 二叉树中的列表 Linked List in Binary Tree

LeetCode Contest 178-1367. 二叉树中的列表 Linked List in Binary Tree


目录

一、中文版

二、英文版

三、My answer

四、解题报告

一、中文版

给你一棵以 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 之间。

二、英文版

 

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:

网络异常,图片无法展示
|

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

Output: true

Explanation: Nodes in blue form a subpath in the binary Tree.  

Example 2:

网络异常,图片无法展示
|

Input: head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Output: true
Example 3:
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]
Output: false
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.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/linked-list-in-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

三、My answer

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 比对是否存在和链表head相同的路径
    def isSame(self, head: ListNode, root: TreeNode) -> bool:
        if not head: return True # 没有剩余链表节点返回真
        if head and root and root.val == head.val: # 当前节点相同,比较左节点或右节点
            return self.isSame(head.next, root.left) or self.isSame(head.next, root.right)
        return False # 当前节点不同,返回假
    def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
        if root: # 当前节点不为空
            # 返回当前节点是否和链表一致,或者左子树是否含有链表路径,或者右子树是否含有链表路径
            return self.isSame(head, root) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right)
        return False

四、解题报告

题目给的节点数不多,可以直接比对二叉树中每一个节点的路径来判断是否和链表中的值一致。

isSubPath isSame 都采用先根遍历即可。

相关文章
|
9月前
|
人工智能 Java
Java 中数组Array和列表List的转换
本文介绍了数组与列表之间的相互转换方法,主要包括三部分:1)使用`Collections.addAll()`方法将数组转为列表,适用于引用类型,效率较高;2)通过`new ArrayList&lt;&gt;()`构造器结合`Arrays.asList()`实现类似功能;3)利用JDK8的`Stream`流式计算,支持基本数据类型数组的转换。此外,还详细讲解了列表转数组的方法,如借助`Stream`实现不同类型数组间的转换,并附带代码示例与执行结果,帮助读者深入理解两种数据结构的互转技巧。
626 1
Java 中数组Array和列表List的转换
|
12月前
|
数据挖掘 大数据 数据处理
python--列表list切分(超详细)
通过这些思维导图和分析说明表,您可以更直观地理解Python列表切分的概念、用法和实际应用。希望本文能帮助您更高效地使用Python进行数据处理和分析。
329 14
|
12月前
|
C语言 Python
[oeasy]python054_python有哪些关键字_keyword_list_列表_reserved_words
本文介绍了Python的关键字列表及其使用规则。通过回顾`hello world`示例,解释了Python中的标识符命名规则,并探讨了关键字如`if`、`for`、`in`等不能作为变量名的原因。最后,通过`import keyword`和`print(keyword.kwlist)`展示了Python的所有关键字,并总结了关键字不能用作标识符的规则。
281 9
|
数据挖掘 大数据 数据处理
python--列表list切分(超详细)
通过这些思维导图和分析说明表,您可以更直观地理解Python列表切分的概念、用法和实际应用。希望本文能帮助您更高效地使用Python进行数据处理和分析。
1086 10
|
测试技术 开发者 Python
在 Python 中创建列表时,应该写 `[]` 还是 `list()`?
在 Python 中,创建列表有两种方法:使用方括号 `[]` 和调用 `list()` 函数。虽然两者都能创建空列表,但 `[]` 更简洁、高效。性能测试显示,`[]` 的创建速度比 `list()` 快约一倍。此外,`list()` 可以接受一个可迭代对象作为参数并将其转换为列表,而 `[]` 则需要逐一列举元素。综上,`[]` 适合创建空列表,`list()` 适合转换可迭代对象。
178 1
在 Python 中创建列表时,应该写 `[]` 还是 `list()`?
|
索引 Python
List(列表)
List(列表)。
222 4
|
JavaScript 数据管理 虚拟化
ArkTS List组件基础:掌握列表渲染与动态数据管理
在HarmonyOS应用开发中,ArkTS的List组件是构建动态列表视图的核心。本文深入探讨了List组件的基础,包括数据展示、性能优化和用户交互,以及如何在实际开发中应用这些知识,提升开发效率和应用性能。通过定义数据源、渲染列表项和动态数据管理,结合虚拟化列表和条件渲染等技术,帮助开发者构建高效、响应式的用户界面。
1021 2
|
NoSQL 关系型数据库 MySQL
Redis 列表(List)
10月更文挑战第16天
178 2
|
JavaScript
DOM 节点列表长度(Node List Length)
DOM 节点列表长度(Node List Length)
|
JavaScript
DOM 节点列表长度(Node List Length)
DOM 节点列表长度(Node List Length)