-
Notifications
You must be signed in to change notification settings - Fork 0
331_VerifyPreorderSerializationofaBinaryTree
a920604a edited this page Apr 14, 2023
·
1 revision
class Solution {
public:
vector<string> split(string words){
vector<string> pre;
string cur;
for(char c:words){
if(c==','){
pre.push_back(cur);
cur.clear();
}
else{
cur+=c;
}
}
if(!cur.empty()) pre.push_back(cur);
return pre;
}
bool isValidSerialization(string preorder) {
vector<string> pre = split(preorder);
int num_child = 1;
for(string str:pre){
num_child--;
if(num_child<0) return false;
if(str!="#"){
num_child+=2;
}
}
return num_child==0;
}
};
- time complexity
O(n)
- space complexity
O(n)
footer