10-line C++ recursion. clean and straighforward


  • 0
    C
        bool helper(stringstream& ss) {
            string token;
            getline(ss, token, ',');
            if(token.empty()) return false;
            if(token == "#") return true;
            return helper(ss) && helper(ss);
        }
        bool isValidSerialization(string preorder) {
            stringstream ss(preorder);
            return helper(ss) && ss.eof(); // need to check .eof() , e.g. corner case 1, #, #, 1 should return false, because ss is not used up.
        }
    

Log in to reply
 

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