41 / 75
M

Count Good Nodes in Binary Tree

#41
treedfs

DFS tracking max value on path; count nodes where node.val >= maxSoFar.

Example:

Input:root=[3,1,4,3,null,1,5]
Output:4 (nodes: 3,4,3,5 are good)

Common Mistakes:

  • Not passing max value down the recursion
  • Using global max instead of path max
  • Forgetting that root is always a good node
  • Off-by-one error with >= vs > comparison

Notes:

Track path maximum during DFS. Time O(n), Space O(h) for recursion stack.

41/75
Count Good Nodes in Binary Tree