<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p>

<p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p>

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p>

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

<p><strong class="example">Example 2:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p>

<pre>
<strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
<strong>Output:</strong> true
</pre>

<p><strong class="example">Example 3:</strong></p>

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

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li>
	<li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li>
	<li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li>
</ul>

# 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:
        list_number_check = []
        list_node = head
        while list_node:
            list_number_check.append(list_node.val)
            list_node = list_node.next

        map_parent = dict()
        map_tree_val = dict()

        def is_same_list_number(map_parent: dict, degree: int, index: int):
            if degree < len(list_number_check) - 1:
                return False
            for i in range(len(list_number_check)-1, -1, -1):
                num_in_list_node = list_number_check[i]
                num_in_tree_node = map_tree_val[index]
                print(num_in_tree_node)
                if num_in_list_node != num_in_tree_node:
                    return False
                index = map_parent.get(index, None)
            return True

        def dfs(node: Optional[TreeNode], map_parent: dict, degree: int, index: int):
            map_tree_val[index] = node.val
            if is_same_list_number(map_parent, degree, index):
                return True
            if node.left:
                left_node = node.left
                left_index = 2*index
                map_parent[left_index] = index
                if dfs(left_node, map_parent, degree+1, left_index):
                    return True
            if node.right:
                right_node = node.right
                right_index = 2*index+1
                map_parent[right_index] = index
                if dfs(right_node, map_parent, degree+1, right_index):
                    return True
            return False

        x = dfs(root, map_parent, 0, 1)
        return x
18200