<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.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
max_distance = 0
class Solution:
    
    def amountOfTime(self, root: Optional[TreeNode], start: int) -> int:
        # if this was a graph with bidirectional nodes, time infected would be number of iterations
        # running bfs with first level being the infected node.

        # Idea: time to infect each node is number of edges away each node is from infected. ex:
        # in Example 1, nodes 9 and 2 are 4 edges away from 3, which are the futhest nodes, hence 
        # why the output is 4. So what we are really looking for is how far away is the furthest
        # node to the infected node. Note that the furthest away node must either be a leaf
        # or the root.

        # Simulate infections: infect(current, parent_distance_from_infected)
        # method has 2 modes: find infected and infect. If distance_from_infected is stated,
        # we have found infected. current distance from infected is just 1 more than that. Check
        # if greater than global max. If it is change global max. Return distance from infected.
        # For nodes that are ancestors of infected, this return value is not useful, for the others
        # it is. Lets say we are at the parent of infected. Is current infected, no, look at children.
        # We find infected on our right and it returns the distance from infected is 0, then we 
        # are 0+1 away from infected. Now we can infect the left side (yes, we will have
        # to go down left side again since first time we didn't tell it the parent distance from 
        # infected, now we know it)
    
        global max_distance
        max_distance = 0
        
        def infect(current, parent_distance_from_infected = None):

            global max_distance
            # adding this so we don't have to check if node has any child missing
            if current is None:
                return None
            

            cur_distance_from_infected = None
            
            if current.val == start:
                cur_distance_from_infected = 0

            elif parent_distance_from_infected is not None:
                cur_distance_from_infected = parent_distance_from_infected + 1
            
            # if we know how far away we are from infected, then descendants can find their distance
            # from infected
            if cur_distance_from_infected is not None:
                if current.left is not None:
                    infect(current.left, cur_distance_from_infected)
                if current.right is not None:
                    infect(current.right, cur_distance_from_infected)
            # if we do NOT know how far from infected, try to find it in either left or right child
            # if we find it in left, we can tell the right child to find their distances
            else:
                left_distance_from_inf = infect(current.left)
                if left_distance_from_inf is not None:
                    infect(current.right, left_distance_from_inf + 1)
                    cur_distance_from_infected = left_distance_from_inf + 1
                
                else:
                    right_dis_from_inf = infect(current.right)
                    if right_dis_from_inf is not None:
                        infect(current.left, right_dis_from_inf + 1)
                        cur_distance_from_infected = right_dis_from_inf + 1

            
            if cur_distance_from_infected is not None and cur_distance_from_infected > max_distance:
                max_distance = cur_distance_from_infected
                
                
     
            return cur_distance_from_infected



        infect(root)
        return max_distance

        
199