: returns count of good nodes in subtree. Add 1 if . Recurse on children with . Sum the results. Base case: null returns . Leaf returns if good, otherwise. Time: , Space: . This is the template for top-down tree DP: carry state down, aggregate results up. This is one of the simpler tree DP problems. It demonstrates the top-down pattern clearly.
Time complexity: .
Space complexity: .