Every integer increases a branch and every '#' decrease a branch, and except the last '#', there should be always at least one branch left. And threre may be integers more than one digits, so just check the one before ','.

```
for(int i = 1; i < n; i++){
if(preorder[i] == ','){
char pre = preorder[i-1];
if(pre=='#')p--;
else p++; //input is assured to be valid
if(p<1)return false;
}
}
return preorder[n-1]=='#'&&p==1;
```