Linked List in Binary Tree Problem
Description
LeetCode Problem 1367.
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:
- The number of nodes in the tree will be in the range
[1, 2500]. - The number of nodes in the list will be in the range
[1, 100]. 1 <= Node.val <= 100for each node in the linked list and binary tree.
Solution
Approach: Try Every Starting Node
A matching path can start at any tree node, so split the problem in two:
matches(node, tree): does the list, from node on, run down a path that starts exactly at tree?
- If
nodeis null, the whole list has been matched:true. - If
treeis null, or its value differs fromnode.val, the path breaks:false. - Otherwise the current values agree, and the rest of the list must continue into one of the children:
matches(node.next, tree.left) || matches(node.next, tree.right).
isSubPath(head, root): the path starts either at root (matches(head, root)) or somewhere inside the left or right subtree, which is the same question asked recursively on root.left and root.right. An empty tree contains no path.
Keep the two functions apart. A common bug is to restart from head inside matches when a value differs: the list must be matched by consecutive nodes, and a restart in the middle would accept broken paths. For the same reason, once a match starts, it can only continue to a child, never jump to a sibling: head = [4,2,8] is not a path in the tree [4,2,8], where 2 and 8 are both children of 4.
Complexity: O(n * m) time, where n is the number of tree nodes and m the length of the list. An attempt that starts at a node only visits nodes at most m levels below it, so each node is visited by at most m attempts: those starting at it or at one of its m - 1 nearest ancestors. O(h) space for the recursion, where h is the height of the tree.
class Solution:
def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool:
def matches(node: Optional[ListNode], tree: Optional[TreeNode]) -> bool:
"""Does the list from node run down a path that starts exactly at tree?"""
if node is None:
return True # the whole list matched
if tree is None or tree.val != node.val:
return False
return matches(node.next, tree.left) or matches(node.next, tree.right)
if root is None:
return False
# Try a path starting at root, else one that starts somewhere in a subtree.
return matches(head, root) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right)