--- ###### tags: `Leetcode` --- # Leetcode 1367. Linked List in Binary Tree [link](https://leetcode.com/problems/linked-list-in-binary-tree/) --- 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: ![](https://i.imgur.com/et7HiMb.png) 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: ![](https://i.imgur.com/fQq4LEJ.png) 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 <= 100 for each node in the linked list and binary tree. --- If the current node in head is empty, the helper function returns True indicating that the linked list is a subpath of the binary tree. If the current node in root is empty, the helper function returns False indicating that the linked list is not a subpath of the binary tree. If the values stored in the current nodes in head and root are not equal, the helper function returns False. Otherwise, the helper function calls itself recursively with the next node in head and either the left or right child node in root, depending on which child node is non-empty. The isSubPath() method then checks if the linked list head is empty. If so, it returns True indicating that the linked list is a subpath of the binary tree. If the binary tree root is empty, the method returns False indicating that the linked list is not a subpath of the binary tree. Finally, the method calls the helper() function with the head and root nodes and returns the result of that function OR the result of calling isSubPath() recursively with the head node and the left child node and with the head node and the right child node in root. #### Solution 1 ```python= # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool: def helper(head, root): if not head: return True if not root: return False if head.val != root.val: return False return helper(head.next, root.left) or helper(head.next, root.right) if not head: return True if not root: return False return helper(head, root) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right) ``` O(T): O(N * M) O(S): O(h)