Leetcode 1544 Solution

This article provides solution to leetcode question 1544 (count-good-nodes-in-binary-tree)

https://leetcode.com/problems/count-good-nodes-in-binary-tree

Solution

# 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:
        ans = 0

        def dfs(node, max_val):
            nonlocal ans

            if not node:
                return

            if node.val >= max_val:
                ans += 1

            dfs(node.left, max(max_val, node.val))
            dfs(node.right, max(max_val, node.val))

        dfs(root, -1000000)

        return ans