-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC0331.cpp
49 lines (46 loc) · 1.41 KB
/
LC0331.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <vector>
#include <string>
#include <stack>
using namespace std;
class Solution {
public:
bool isValidSerialization(string preorder) {
vector<string> input;
string tmp = "";
for (int i=0; i<preorder.size(); i++) {
if (preorder[i] == ',') {
input.push_back(tmp);
tmp = "";
} else {
tmp += preorder[i];
}
}
if (tmp.size() > 0) {
input.push_back(tmp);
}
vector<string> tree;
for (int i=0; i<input.size(); i++) {
if (input[i] == "#") {
while (tree.size()>=2 && tree[tree.size()-1]=="#" && tree[tree.size()-2]!="#") {
tree.pop_back();
tree.pop_back();
}
tree.push_back(input[i]);
} else {
tree.push_back(input[i]);
}
}
return tree.size()==1 && tree[0]=="#";
}
bool isDigit(char ch) {
return '0' <= ch && ch <= '9';
}
};
int main() {
Solution s;
cout << s.isValidSerialization("9,3,4,#,#,1,#,#,2,#,6,#,#") << endl;
cout << s.isValidSerialization("1,#") << endl;
cout << s.isValidSerialization("9,#,#,1") << endl;
return 0;
}