C++ stack simple


  • 0
    R

    class Solution {
    public:
    bool isValidSerialization(string preorder) {
    stack<string >s;
    vector<string> vs;
    string substr;
    stringstream ss(preorder);
    while(ss.good()){
    getline(ss,substr,',');
    vs.push_back(substr);
    }
    for(auto c: vs){
    if(c!="#") s.push(c);
    else{
    while(!s.empty()&&s.top()=="#"){
    s.pop();
    if(s.empty()) return 0;
    s.pop();
    }
    s.push("#");

            }
        }
        //cout<<s.empty()<<" "<<s.top()<<" "<<s.size();
        if(s.empty()||(s.size()==1&&s.top()=="#")) return 1;
        else return 0;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.