Count good nodes
Mediumtreedfs
A node is good if no node on the path from the root to it has a greater value. The root is always good. Return how many nodes are good.
Examples write a tree level by level, with null for a missing child: [3,9,20,null,null,15,7].
Example 1
Input: root = [3,1,4,3,null,1,5] Output: 4
Example 2
Input: root = [3,3,null,4,2] Output: 3
Constraints
- 1 ≤ number of nodes ≤ 10⁵
- -10⁴ ≤ Node.val ≤ 10⁴
Submit checks your solution against 8 tests, including hidden ones.
// Provided by the judge: keep these definitions
class TreeNode {
constructor(val = 0, left = null, right = null) {
this.val = val
this.left = left
this.right = right
}
}
/**
* @param {TreeNode|null} root
* @return {int}
*/
function goodNodes(root) {
}