Count Good Nodes in Binary Tree
Recognize the pattern
Brute force idea
A straightforward first read of Count Good Nodes in Binary Tree is this: For each node, check all ancestors. Re-traverses the path to root for every node. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.
Better approach
A calmer way to see Count Good Nodes in Binary Tree is this: DFS passing the maximum value seen so far from root to current node. If current node ≥ maxSoFar, it's a 'good' node. Update maxSoFar for children. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.
Key invariant
The truth you want to protect throughout Count Good Nodes in Binary Tree is this: maxSoFar always holds the largest value on the path from root to the current node. A node is 'good' if and only if its value ≥ maxSoFar. If that remains true after every update, the rest of the reasoning has a stable place to stand.
Watch out for
The trap in Count Good Nodes in Binary Tree usually looks like this: Updating maxSoFar globally instead of per-path — each path from root has its own running maximum. Pass it as a parameter, don't store it as shared state. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.