331. Verify Preorder Serialization of a Binary Tree 發表於 2023-02-13 | 分類於 leetcode problemsolution12345678910111213141516171819202122232425262728293031class 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; }}; analysis time complexity O(n) space complexity O(n)