DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Tree

Count Good Nodes in Binary Tree

**Problem Statement:** Given the required input arguments, write an efficient algorithm to solve the **Count Good Nodes in Binary Tree** problem. Implement the required logic as specified by standard definitions for this classic algorithmic challenge. **Hint / Expected Approach:** DFS tracking max-so-far on path to node **Edge Cases to Consider:** - (1) All nodes same value - (2) Strictly decreasing from root - (3) Single node

Examples

Example 1
Input: [3,1,4,3,null,1,5]
Output: 4
Explanation: Nodes 3,4,3,5 are good
Example 2
Input: [3,3,null,4,2]
Output: 3
Explanation: Nodes 3,3,4
Example 3
Input: [1]
Output: 1
Explanation: Root is always good

Constraints

  • ▪The number of nodes in the tree is in the range [0, 2000]
  • ▪-1000 <= Node.val <= 1000
  • ▪Ensure depth-first or breadth-first traversals are memory-efficient

Watch Out For Edge Cases

  • ▪All nodes same value
  • ▪Strictly decreasing from root
  • ▪Single node
Frequently Asked At
AmazonGoogleMicrosoft