```
public class Solution {
Stack<String> stack = new Stack();
public boolean isValidSerialization(String preorder) {
String[] order = preorder.split(",");
return (check(order, 0) == order.length);
}
private int check(String[] order, int cur) {
if (cur >= order.length) return -1;
if (!order[cur].equals("#")) {
stack.push(order[cur]);
int res = check(order, cur + 1);
if (res < 0) return res;
stack.pop();
return check(order, res);
} else {
return cur + 1;
}
}
```

}