3ms C++ code


  • 0
    S
    class Solution{
    public:
          bool isValidSerialization(string preorder){
               string SS="";
               for(int i=0;i<preorder.size();i++){
                    if(preorder[i]!='#'){
                           SS+='N';
                            while(preorder[i]!=',')i++;
                    }else{
                            SS+='#';
                            while(SS.size()>=2&&SS.back()=='#'&&SS[SS.size()-2]=='#'){
                                 if(SS.size()==2||SS[SS.size()-3]=='#')return false;
                                 SS.pop_back();
                                 SS.pop_back();
                                 SS.pop_back();
                                 SS+='#';
                            }
                            i++;}
                }
                return SS=="#";
           }
     };

  • 0

    Wrong answer even for the example case...


  • 0
    S

    sorry a typo, should be
    while(preorder[i]!=',')i++;
    instead of
    while(preorder[i]==',')i++;


Log in to reply
 

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