Count Nodes That Match or Exceed Every Ancestor. A native binary-tree node is good when no value on its root-to-node path is greater than its own value.
### Problem
A native binary-tree node is good when no value on its root-to-node path is greater than its own value. Return the number of good nodes, counting the root and allowing equality with ancestors.
```cpp
class Solution {
public:
int goodNodes(TreeNode* root);
};
```
### Examples
**Example 1:**
```text
Input: root = [2,2,1,null,3]
Output: 3
Explanation: The root2,the left child2,and its child3 are good. The right child1 is below its ancestor2.
```
**Example 2:**
```text
Input: root = [-2,-3,-1]
Output: 2
Explanation: The root-2 and right child-1 qualify; left child-3 does not.
```
### Constraints
- The binary tree contains 1 to 100000 nodes.
- Every value lies in `[-10000,10000]`.
### Contract considerations
Preserve the TreeNode pointer interface; level-order arrays describe the examples only.
```hint Read the boundary condition
Compare a node against its full root-to-node path, allowing equality. Include negative values and the root itself.
```
### Boundary reminders
Preserve the TreeNode pointer interface; level-order arrays describe the examples only.
Overview: Count Nodes That Match or Exceed Every Ancestor. A native binary-tree node is good when no value on its root-to-node path is greater than its own value.
A native binary-tree node is good when no value on its root-to-node path is greater than its own value. Return the number of good nodes, counting the root and allowing equality with ancestors.
class Solution {
public:
int goodNodes(TreeNode* root);
};
Examples
Example 1:
Input: root = [2,2,1,null,3]
Output: 3
Explanation: The root2,the left child2,and its child3 are good. The right child1 is below its ancestor2.
Example 2:
Input: root = [-2,-3,-1]
Output: 2
Explanation: The root-2 and right child-1 qualify; left child-3 does not.
Constraints
The binary tree contains 1 to 100000 nodes.
Every value lies in
[-10000,10000]
.
Contract considerations
Preserve the TreeNode pointer interface; level-order arrays describe the examples only.
Boundary reminders
Preserve the TreeNode pointer interface; level-order arrays describe the examples only.