<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
class Solution:

    def amountOfTime(self, root: Optional[TreeNode], start: int) -> int:
        self.tmax = 0
        self.bmax = 0
        x = self.helper(root,start)
        # print(self.tmax,self.bmax,x)
        return max(self.tmax,self.bmax)

    def helper(self,root,start):
        if root == None:
            return [0, False] #0 or 1
        left = self.helper(root.left, start)
        right = self.helper(root.right, start)
        if root.val == start:
            self.bmax = max(left[0],right[0])
            return [1, True] #0 or 1
        if left[1]:
            self.tmax = max(self.tmax, left[0]+right[0]) #what to return? consider left child or not
            return [left[0]+1, True]
        if right[1]:
            self.tmax = max(self.tmax, left[0]+right[0]) #what to return? consider left child or not
            return [right[0]+1, True]
        return [max(left[0],right[0]) + 1, False]
    # def helper(self, root, start):
    #     if root == None:
    #         return 0
    #     isFound = False
    #     lheight = self.helper(root.left, start)
    #     rheight = self.helper(root.right, start)
    #     ans = max(abs(lheight),abs(rheight))+1
    #     if root.val == start:
    #         self.dheight = ans
    #         return -1
    #     if lheight<0 or rheight<0:
    #         return ans*-1
    #     return ans

    # def amountOfTime(self, root: Optional[TreeNode], start: int) -> int:
    #     self.dheight = 0
    #     self.uheight = 0
    #     ans = self.helper(root,start)
    #     print(ans,self.dheight)
    #     return max(abs(ans),self.dheight)
    # def helper(self, root, start):
    #     if root == None:
    #         return 0
    #     lheight = self.helper(root.left, start)
    #     rheight = self.helper(root.right, start)
    #     ans = max(abs(lheight),abs(rheight))+1
    #     if root.val == start:
    #         self.dheight = ans
    #         return -1
    #     if lheight<0 or rheight<0:
    #         return ans*-1
    #     return ans

    # def helper(self, root, start):
    #     if root == None:
    #         return 0
    #     if
    #     lheight = self.helper(root.left, start)
    #     rheight = self.helper(root.right, start)
    #     ans = 0
    #     if root.value == start:
    #         ans = max(lheight, rheight) + 1
    #         self.dheight = ans
    #         return -1*ans

50377