Skip to content

Commit de252d7

Browse files
Create Count Good Nodes in Binary Tree
Added Create Count Good Nodes in Binary Tree in java
1 parent a163816 commit de252d7

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
3+
public int goodNodes(TreeNode root) {
4+
return helper(root, -99999);
5+
}
6+
7+
public int helper(TreeNode root, int max) {
8+
if (root == null) return 0;
9+
10+
int res = root.val >= max ? 1 : 0;
11+
12+
res += helper(root.left, Math.max(root.val, max));
13+
res += helper(root.right, Math.max(root.val, max));
14+
15+
return res;
16+
}
17+
}

0 commit comments

Comments
 (0)