#1367

Linked List in Binary Tree

specialist · 835 · lc medium +31 · failed · 51.9% accepted · 3,024 likes · top 41%

Description

Given a binary tree rooted at root and a singly linked list starting at head, determine whether the linked list's values appear along some downward path in the tree (starting at any tree node and moving toward its descendants). Return True if such a path exists, False otherwise.

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.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14