10 line easy c++ solution


  • 5
    Z
     bool isValidSerialization(string preorder) {
            if(preorder.empty())return false;
            int cnt=1,i=0;
            while(i<preorder.size()){
                if(preorder[i]=='#')cnt--;
                else cnt++;
                if(cnt==0)break;
                while(i<preorder.size()&&preorder[i++]!=',');
            }
            return cnt==0&&i==preorder.size()-1;
        }

Log in to reply
 

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