-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountGoodNodesInBinaryTree1448.java
More file actions
45 lines (39 loc) · 1.33 KB
/
Copy pathCountGoodNodesInBinaryTree1448.java
File metadata and controls
45 lines (39 loc) · 1.33 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X.
Return the number of good nodes in the binary tree.
*/
public class CountGoodNodesInBinaryTree1448 {
public static void main(String[] args) {
System.out.println(goodNodes(new TreeNode(3, new TreeNode(1, new TreeNode(3), null), new TreeNode(4, new TreeNode(1), new TreeNode(5)))));
System.out.println(goodNodes(new TreeNode(3, new TreeNode(3, new TreeNode(4), new TreeNode(2)), null)));
}
public static int goodNodes(TreeNode root) {
return countGoodNodes(root, root.val);
}
public static int countGoodNodes(TreeNode node, int max) {
if (node == null) return 0;
int count = 0;
if (node.val >= max) {
count++;
max = node.val;
}
count += countGoodNodes(node.left, max);
count += countGoodNodes(node.right, max);
return count;
}
public static 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;
}
}
}