Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <sstream>
- #include <stack>
- class Solution {
- public:
- bool isValidSerialization(std::string preorder) {
- std::istringstream iss(preorder);
- std::string node;
- std::stack<std::string> stack;
- while (std::getline(iss, node, ',')) {
- while (node == "#" && !stack.empty() && stack.top() == "#") {
- stack.pop();
- if (stack.empty()) {
- return false;
- }
- stack.pop();
- }
- stack.push(node);
- }
- return stack.size() == 1 && stack.top() == "#";
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement