<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p>

<p>Return the number of <strong>good</strong> nodes in the binary tree.</p>

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

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/04/02/test_sample_1.png" style="width: 263px; height: 156px;" /></strong></p>

<pre>
<strong>Input:</strong> root = [3,1,4,3,null,1,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Nodes in blue are <strong>good</strong>.
Root Node (3) is always a good node.
Node 4 -&gt; (3,4) is the maximum value in the path starting from the root.
Node 5 -&gt; (3,4,5) is the maximum value in the path
Node 3 -&gt; (3,1,3) is the maximum value in the path.</pre>

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

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/04/02/test_sample_2.png" style="width: 157px; height: 161px;" /></strong></p>

<pre>
<strong>Input:</strong> root = [3,3,null,4,2]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Node 2 -&gt; (3, 3, 2) is not good, because &quot;3&quot; is higher than it.</pre>

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

<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Root is considered as <strong>good</strong>.</pre>

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

<ul>
	<li>The number of nodes in the binary tree is in the range&nbsp;<code>[1, 10^5]</code>.</li>
	<li>Each node&#39;s value is between <code>[-10^4, 10^4]</code>.</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 goodNodes(self, root: TreeNode) -> int:
        '''
        Clarifying Questions and Concerns:
            - If a root is null, can I assume I should return 0?
                - yes.

            - The root is considered good because there are no nodes before it that are greater than it.
            - A node X is considered a 'good' node when its value is greater than or equal to all the nodes in the 
                path from ROOT to node X.

            - Input: a root representing a binary tree.
            - Output: an integer representing the number of 'good' nodes in the binary tree.
        
        Brainstorm:
            Example:
                        [3, -inf] 
                          /   \
                      [1, 3]  [4, 3]
                       /       / \
                     [3, 3]  [1, 3]   [5, 4]
                     
                      good_nodes = [3, 4, 5]

                        [30, -inf] 
                          /   \
                      [1, 30]  [40, 30]
                       /          /     \
                     [31, 30] [100, 40] [50, 40]
                                /
                            [90, 100]
                                \
                                [80, 100]
                                    \
                                    [101, 100]

                      good_nodes = [30, 31, 40, 100, 40, 101]

                State Variables
                    root: TreeNode, largest_so_far: integer
                
                "Global" Variables
                    good_nodes: integer

            Approach:
                init good_nodes array 
                define helper(root, largest_so_far):
                    base case
                        if root is null
                            return
                    recursive case
                        if root's val >= largest_so_far:
                            add root's vall to good_nodes array
                            set largest_so_far to root's val
                        
                        call helper on root's left and pass in largest_so_far
                        call helper on root's right and pass in largest_so_far
                
                invoke helper and pass in root and negative infinity

                return good_nodes array

            Runtime: O(n) where n is the number od nodes in the tree.
            Space: O(n) where n is the number of nodes in the tree and the shape of the tree is a linked list or we store all nodes.
        '''
        good_nodes = []
        def dfsHelper(node, largest_so_far):
            if not node:
                return
            
            if node.val >= largest_so_far:
                good_nodes.append(node)
                largest_so_far = max(largest_so_far, node.val)

            dfsHelper(node.left, largest_so_far)
            dfsHelper(node.right, largest_so_far)
        
        dfsHelper(root, float('-inf'))
        return len(good_nodes)
30400