-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path331.cpp
More file actions
51 lines (49 loc) · 1.23 KB
/
331.cpp
File metadata and controls
51 lines (49 loc) · 1.23 KB
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
50
51
class Solution {
public:
bool isValidSerialization(string preorder) {
int nodes = 1;
stringstream ss(preorder);
string token;
while (getline(ss, token, ',')) {
nodes--;
if (nodes < 0) return false;
if (token != "#") nodes += 2;
}
return nodes == 0;
}
};
// V2
// class Solution {
// public:
// int index = 0;
// bool flag = true;
// void build(vector<string>& nodes) {
// if (!flag) return;
// if (nodes[index] == "#") {
// index++;
// return;
// }
// index++;
// if (index == nodes.size()) {
// flag = false;
// return;
// }
// build(nodes);
// if (index == nodes.size()) {
// flag = false;
// return;
// }
// build(nodes);
// }
// bool isValidSerialization(string preorder) {
// vector<string> nodes;
// string token;
// stringstream ss(preorder);
// while (getline(ss, token, ',')) {
// nodes.push_back(token);
// }
// build(nodes);
// if (index != nodes.size()) flag = false;
// return flag;
// }
// };