-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLeetCode971.java
More file actions
43 lines (37 loc) · 1.02 KB
/
LeetCode971.java
File metadata and controls
43 lines (37 loc) · 1.02 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
package problems;
import java.util.ArrayList;
import java.util.List;
public class LeetCode971 {
List<Integer> flipped;
int index;
int[] voyage;
public List<Integer> flipMatchVoyage(TreeNode root, int[] voyage) {
flipped = new ArrayList();
index = 0;
this.voyage = voyage;
dfs(root);
if (!flipped.isEmpty() && flipped.get(0) == -1) {
flipped.clear();
flipped.add(-1);
}
return flipped;
}
public void dfs(TreeNode node) {
if (node != null) {
if (node.val != voyage[index++]) {
flipped.clear();
flipped.add(-1);
return;
}
if (index < voyage.length && node.left != null &&
node.left.val != voyage[index]) {
flipped.add(node.val);
dfs(node.right);
dfs(node.left);
} else {
dfs(node.left);
dfs(node.right);
}
}
}
}