-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm1448.java
31 lines (31 loc) · 888 Bytes
/
m1448.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int goodNodes(TreeNode root) {
return goodNodesHelper(root, Integer.MIN_VALUE);
}
private int goodNodesHelper(TreeNode curr, int maxPrevious) {
int output = (curr.val >= maxPrevious) ? 1 : 0;
int maxx = Integer.max(curr.val, maxPrevious);
if (curr.left != null) {
output += goodNodesHelper(curr.left, maxx);
}
if (curr.right != null) {
output += goodNodesHelper(curr.right, maxx);
}
return output;
}
}