用队列也行
class Solution {
public:
bool isValidSerialization(string preorder) {
char str[preorder.size()+1];
for(int i = 0;i<preorder.size();i++) str[i] = preorder[i];
str[preorder.size()] = '\0';
char * p = strtok(str,",");
vector<string>word;
while(p)
{
string t = p;
word.push_back(t);
p = strtok(NULL,",");
}
if(word[0] == "#")
{
if(word.size() > 1) return false;
else return true;
}
queue<string>q;
q.push(word[0]);
int i = 0;
while(!q.empty())
{
q.pop();
if(i+1<word.size())
{
if(word[i+1]!="#") q.push(word[i+1]);
i++;
}
else return false;
if(i+1<word.size())
{
if(word[i+1]!="#") q.push(word[i+1]);
i++;
}
else return false;
}
cout<<i<<endl;
if(i!=word.size()-1) return false;
return true;
}
};