class Solution {
fun goodNodes(root: TreeNode?): Int {
var good = 0
good += dfs(root, root!!.`val`) // number of nodes >= 1
return good
}
fun dfs(root: TreeNode?, maxValueInPath: Int): Int {
root ?: return 0
var temp = 0
var newMaxVal = maxValueInPath
if (root.`val` >= maxValueInPath) {
temp++
newMaxVal = root.`val`
}
temp += dfs(root.left, newMaxVal)
temp += dfs(root.right, newMaxVal)
return temp
}
}