-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountGoodNodesInBinaryTree.java
59 lines (51 loc) · 1.81 KB
/
CountGoodNodesInBinaryTree.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package Algorithms.BinaryTrees;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 26 April 2025
*
* 3*
* / \
* 1 4*
* / / \
* 3* 1 5*
* /
* 5*
*
* Here, the nodes marked with * are good nodes.
*/
public class CountGoodNodesInBinaryTree {
static class TreeNode {TreeNode left, right; int val; TreeNode(int x) {val = x;}}
public static void main(String[] args) {
TreeNode root = new TreeNode(3);
root.left = new TreeNode(1);
root.right = new TreeNode(4);
root.left.left = new TreeNode(3);
root.right.left = new TreeNode(1);
root.right.left.left = new TreeNode(5);
root.right.right = new TreeNode(5);
System.out.println("goodNodes(root) => " + goodNodes(root));
System.out.println("goodNodesMyApproach(root) => " + goodNodesMyApproach(root));
}
public static int goodNodes(TreeNode root) {
return dfs(root, root.val);
}
private static int dfs(TreeNode node, int max) {
if (node == null) return 0;
int count = node.val >= max ? 1 : 0;
max = Math.max(max, node.val);
count += dfs(node.left, max) + dfs(node.right, max);
return count;
}
static int c = 0;
public static int goodNodesMyApproach(TreeNode root) {
dfs2(root, root.val);
return c;
}
private static void dfs2(TreeNode x, int maxVal) {
if(x == null) return;
if(x.val >= maxVal) c++;
maxVal = Math.max(maxVal, x.val);
dfs2(x.left, maxVal);
dfs2(x.right, maxVal);
}
}