<p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>

<p>Each minute, a node becomes infected if:</p>

<ul>
	<li>The node is currently uninfected.</li>
	<li>The node is adjacent to an infected node.</li>
</ul>

<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>

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

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
	<li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
	<li>Each node has a <strong>unique</strong> value.</li>
	<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>

/**
 * 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:
    int counter = 0;
    void solve(TreeNode* root, int start, unordered_map<TreeNode*, TreeNode*>& child_to_parent) {
        if (root == NULL) {
            return;
        }

     
        TreeNode* leftChild = root->left;
        TreeNode* rightChild = root->right;

        if (leftChild != NULL){
            child_to_parent[leftChild] = root;
            solve(root->left, start, child_to_parent);
        }
            

        if (rightChild != NULL){
            child_to_parent[rightChild] = root;
            solve(root->right, start, child_to_parent);
        }
    }

    TreeNode* findNode(TreeNode* root, int start) {
        if(root == NULL){
            return NULL;
        }
        if(root->val == start){
            return root;
        }

        TreeNode* leftAns = findNode(root->left, start);

        if(leftAns != NULL){
            return leftAns;
        }

        return findNode(root->right, start);
    }
    void levelOrderTraversal(TreeNode* root, int start,  unordered_map<TreeNode*, TreeNode*>& child_to_parent){
        if(root == NULL){
            return;
        }

        unordered_map<TreeNode*, bool> visited;

        queue<TreeNode*> q;
        TreeNode* node = findNode(root, start);
        q.push(node);
        visited[node] = true;
        q.push(NULL);

        while(!q.empty()){
            TreeNode* getFront = q.front();
            q.pop();

            if(getFront == NULL){
                cout << endl;                
                counter++;

                if(!q.empty()){
                    q.push(NULL);
                }
            }else{
                cout << getFront->val << " ";

                if(getFront->left != NULL && !visited[getFront->left]){
                    q.push(getFront->left);
                    visited[getFront->left] = true;
                }

                if(getFront->right != NULL && !visited[getFront->right]){
                    q.push(getFront->right);
                    visited[getFront->right] = true;
                }
                
                if(child_to_parent.find(getFront) != child_to_parent.end()){
                    if(!visited[child_to_parent[getFront]]){
                        q.push(child_to_parent[getFront]);
                        visited[child_to_parent[getFront]] = true;
                    }                    
                }
            }
        }
    }
    int amountOfTime(TreeNode* root, int start) {
        // infection starts from 3 at 0 min
        // at 1 minutes adjacents will be infected 1,10,6
        // at 2 minutes 5 will be infected
        // at 3 minutes 4 will be infected
        // at 4 minutes 9,2 will be infected
        // output will be 4 complete tree infected at in 4 minutes

        // How to traverse back? 
        // Infection spreading level by level

        unordered_map<TreeNode*, TreeNode*> child_to_parent;
        solve(root, start, child_to_parent);

        levelOrderTraversal(root, start, child_to_parent);
        return counter - 1; // why -1 because starting of 0 minute first endl will not include after 3
    }
};
217100