My simple 2 c++ solutions


  • 0
    K
    /*solution one
    class Solution {
    public:
    bool isValidSerialization(string preorder) {
        vector<string>arr;
        stringstream ss(preorder);
        while(ss.good()){
            string str;
            getline(ss,str,',');
            arr.push_back(str);
            int size=arr.size();
            while(size>=3 && arr[size-1]=="#" &&arr[size-2]=="#" && arr[size-3]!="#")
            {
                arr.pop_back();
                arr.pop_back();
                arr.pop_back();
                arr.push_back("#");
                size=arr.size();
            }
        }
        
        return arr.size()==1&&arr.back()=="#";
    }
    

    };*/

    /*solution two */
        class Solution {
        public:
        bool isValidSerialization(string preorder) {
        vector<string>arr;
        stringstream ss(preorder);
        int diff=1;
        while(ss.good()){
            string str;
            getline(ss,str,',');
            diff--;
            if(diff<0)return false;
            if(str!="#")diff+=2;
        }
        
        return diff==0;
    }
    

    };


Log in to reply
 

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