class Solution {
public:
bool isValidSerialization(string preorder) {
int len = preorder.length();
int i = 0;
stack<int> stk;
stk.push(1);
while(i<len) {
if(stk.empty()) {
return false;
}
if(preorder[i]==',')i++;
else if(preorder[i]=='#') {
stk.top()--;
if(stk.top()==0) stk.pop();
i++;
}
else {//是数字,并找到数字末尾
while(i<len&&preorder[i]!=',') {
i++;
}
stk.top()--;
if(stk.top()==0)stk.pop();
stk.push(2);
}
}
return stk.empty();
}
};
02-15
9088
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)