Imitate the process of deserialize.


  • 0
    R
    class Solution {
    public:
        bool isValidSerialization(string preorder) {
            istringstream ss(preorder);
            bool ret = isValide(ss);
            if (ret)
                return ss.eof();
            return false;
        }
    
        bool isValide(istringstream& ss) {
            if (!ss) return false;
            string str;
            getline(ss, str, ',');
            if (str == "#") return true;
            bool left = isValide(ss);
            bool right = isValide(ss);
            return left && right;
        }
    };

Log in to reply
 

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