Count Good Nodes in Binary Tree
Signals to notice
Brute force first
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.
The key insight
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.
What must stay true
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.
Easy way to go wrong
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.