Count Good Nodes in Binary Tree — C# Coding Problem
Difficulty: medium | Category: tree
Problem Description
Given a binary tree root, a node `X` in the tree is named **good** if in the path from root to `X` there are no nodes with a value greater than `X`. Return the number of good nodes in the binary tree. For this problem, represent the binary tree as a level-order array where `-1` means null. **Hint:** Use depth-first search, tracking the maximum value seen on the path from root to current node.
Examples
Example 1
Input: root = [3,1,4,3,-1,1,5]
Output: 4
Explanation: Nodes 3 (root), 4, 3, and 5 are good.
Example 2
Input: root = [3,3,-1,4,2]
Output: 3