We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 8995388 + eb3b57d commit 8eb6bf6Copy full SHA for 8eb6bf6
LeetCode/1448. Count Good Nodes in Binary Tree.kt
@@ -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