Count Good Nodes in Binary Tree

Problem Summary

Approach Taken

Main Concepts Used

Time & Space Complexity

Code

def dfs(self, root, maxSoFar):
    if not root:
        return 0

    isGood = 1 if root.val >= maxSoFar else 0
    newMax = max(maxSoFar, root.val)

    return (
        isGood +
        self.dfs(root.left, newMax) +
        self.dfs(root.right, newMax)
    )