-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
30 lines (30 loc) · 935 Bytes
/
Solution.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
public class Solution {
public boolean isValidSerialization(String preorder) {
String[] strs=preorder.split(",");
List<String> list=new ArrayList<String>();
for (int i = 0; i < strs.length; i++) {
list.add(strs[i]);
}
return isValid(list);
}
public boolean isValid(List<String> list) {
int len=list.size();
if (len==1) {
return "#".equals(list.get(0));
}
for (int i = 0; i < len-2; i++) {
if ("#".equals(list.get(i+1))&&"#".equals(list.get(i+2))) {
if ("#".equals(list.get(i))) {
return false;
}else{
list.remove(i+2);
list.remove(i+1);
list.remove(i);
list.add(i,"#");
return isValid(list);
}
}
}
return false;
}
}