-
Notifications
You must be signed in to change notification settings - Fork 43
/
CountGoodNodesInBinaryTree.java
162 lines (129 loc) · 4.25 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package LeetCodeJava.Tree;
// https://leetcode.com/problems/count-good-nodes-in-binary-tree/
import LeetCodeJava.DataStructure.TreeNode;
import java.util.*;
/**
* 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;
* }
* }
*/
public class CountGoodNodesInBinaryTree {
// V0
// IDEA : DFS + maintain min value
private int numGoodNodes = 0;
public int goodNodes(TreeNode root) {
//dfs(root, Integer.MIN_VALUE);
findGoodNode(root, root.val); // can use root.val as well
return numGoodNodes;
}
private void findGoodNode(TreeNode node, int maxSoFar) {
if (maxSoFar <= node.val) {
numGoodNodes += 1; // found a good node
}
if (node.right != null) {
/** NOTE !!!
*
* maintian the "max so far" value,
* so instead of comparing all nodes in path
* -> ONLY need to compare current node val with maxSoFar
*/
findGoodNode(node.right, Math.max(node.val, maxSoFar));
}
if (node.left != null) {
/** NOTE !!!
*
* maintian the "max so far" value,
* so instead of comparing all nodes in path
* -> ONLY need to compare current node val with maxSoFar
*/
findGoodNode(node.left, Math.max(node.val, maxSoFar));
}
}
// V1
// IDEA : DFS
// https://leetcode.com/problems/count-good-nodes-in-binary-tree/editorial/
private int numGoodNodes_1 = 0;
public int goodNodes_2(TreeNode root) {
dfs(root, Integer.MIN_VALUE);
return numGoodNodes_1;
}
private void dfs(TreeNode node, int maxSoFar) {
if (maxSoFar <= node.val) {
numGoodNodes_1++;
}
if (node.right != null) {
dfs(node.right, Math.max(node.val, maxSoFar));
}
if (node.left != null) {
dfs(node.left, Math.max(node.val, maxSoFar));
}
}
// V2
// IDEA : DFS + Iterative
// https://leetcode.com/problems/count-good-nodes-in-binary-tree/editorial/
class Pair {
public TreeNode node;
public int maxSoFar;
public Pair(TreeNode node, int maxSoFar) {
this.node = node;
this.maxSoFar = maxSoFar;
}
}
public int goodNodes_3(TreeNode root) {
int numGoodNodes = 0;
Stack<Pair> stack = new Stack<>();
stack.push(new Pair(root, Integer.MIN_VALUE));
while (stack.size() > 0) {
Pair curr = stack.pop();
if (curr.maxSoFar <= curr.node.val) {
numGoodNodes++;
}
if (curr.node.left != null) {
stack.push(new Pair(curr.node.left, Math.max(curr.node.val, curr.maxSoFar)));
}
if (curr.node.right != null) {
stack.push(new Pair(curr.node.right, Math.max(curr.node.val, curr.maxSoFar)));
}
}
return numGoodNodes;
}
// V3
// IDEA : BFS
// https://leetcode.com/problems/count-good-nodes-in-binary-tree/editorial/
class Pair2 {
public TreeNode node;
public int maxSoFar;
public Pair2(TreeNode node, int maxSoFar) {
this.node = node;
this.maxSoFar = maxSoFar;
}
}
public int goodNodes_4(TreeNode root) {
int numGoodNodes = 0;
Queue<Pair> queue = new LinkedList<>();
queue.offer(new Pair(root, Integer.MIN_VALUE));
while (queue.size() > 0) {
Pair curr = queue.poll();
if (curr.maxSoFar <= curr.node.val) {
numGoodNodes++;
}
if (curr.node.right != null) {
queue.offer(new Pair(curr.node.right, Math.max(curr.node.val, curr.maxSoFar)));
}
if (curr.node.left != null) {
queue.offer(new Pair(curr.node.left, Math.max(curr.node.val, curr.maxSoFar)));
}
}
return numGoodNodes;
}
}