Sharing my 12ms C++ solution


  • 1
    T
    class Solution {
    private:
        vector<string> parse(string s)
        {
            istringstream is(s);
            string tmp;
            vector<string> result;
            while(getline(is, tmp, ','))
                result.push_back(tmp);
                
            return result;
        }
        
    public:
        bool isValidSerialization(string preorder) {
            vector<string> strVector = parse(preorder);
            int nullCount=0, valueCount=0;
            for(int i=0; i<strVector.size(); i++)
            {
                if(strVector[i]=="#")
                    nullCount++;
                else
                    valueCount++;
                    
                if( nullCount==valueCount+1 && i != (strVector.size()-1) )
                    return false;
            }
            
            return (nullCount==valueCount+1);
        }
    };

Log in to reply
 

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