<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.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isSubPath(ListNode* head, TreeNode* root) 
    {
        vector<int> linked_list;
        while(head != NULL)
        {
            linked_list.push_back(head->val);
            head = head->next;
        }

        vector<int> valid_indicies;
        
        stack<vector<int>> valid_indicies_stack;
        stack<TreeNode*> treenode_stack;
        TreeNode* node = root;
        bool back_tracked = false;

        while(node)
        {
            if(!back_tracked)
            {
                for(int i = 0; i < valid_indicies.size(); i++)
                {
                    valid_indicies[i] = valid_indicies[i] != -1 && node->val == linked_list[valid_indicies[i] + 1] ? valid_indicies[i] + 1 : -1;
                    if(valid_indicies[i] + 1 == linked_list.size()) return true;
                }
                if(node->val == linked_list[0]) valid_indicies.push_back(0);
                if(node->val == linked_list[0] && linked_list.size() == 1) return true;
            }

            if(node->left && !back_tracked)
            {
                treenode_stack.push(node);
                valid_indicies_stack.push(vector<int>(valid_indicies));

                node = node->left;
                back_tracked = false;
            }
            else if(node->right)
            {
                node = node->right;
                back_tracked = false;
            }
            else if(!treenode_stack.empty())
            {
                node = treenode_stack.top();
                valid_indicies = valid_indicies_stack.top();
                treenode_stack.pop();
                valid_indicies_stack.pop();
                back_tracked = true;
            }
            else
            {
                node = NULL;
            }
        }
        return false;
    }
};
32000