My c++ code, easy to understand


  • 0
    M
    class Solution {
    public:
        bool isValidSerialization(string preorder) {
            vector<string> s;
            int diff = 1;
            vector<string> temp;
            while(1)
            {
                if(preorder.find(',') == std::string::npos)
                {
                temp.push_back(preorder);
                break;
                }
                else
                {
                int position = preorder.find(',');
                temp.push_back(preorder.substr(0,position));
                preorder = preorder.substr(position+1);
                }
            }
            for(string c : temp)
            {
                if(--diff < 0)
                return false;
                if(c != "#")
                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.