Skip to content

Commit 8eb6bf6

Browse files
committed
2 parents 8995388 + eb3b57d commit 8eb6bf6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Example:
3+
* var ti = TreeNode(5)
4+
* var v = ti.`val`
5+
* Definition for a binary tree node.
6+
* class TreeNode(var `val`: Int) {
7+
* var left: TreeNode? = null
8+
* var right: TreeNode? = null
9+
* }
10+
*/
11+
class Solution {
12+
fun goodNodes(root: TreeNode): Int {
13+
var res = 0
14+
fun findGood(node: TreeNode? = root, max: Int = root.`val`) {
15+
if (node == null) return
16+
var curMax = max
17+
if (node.`val` >= curMax) {
18+
curMax = node.`val`
19+
res++
20+
}
21+
findGood(node.left, curMax)
22+
findGood(node.right, curMax)
23+
}
24+
findGood()
25+
return res
26+
}
27+
}

0 commit comments

Comments
 (0)